简体   繁体   English

为什么在printf中\\ n不打印char数组的值或旧值?

[英]why \n in printf prints no value or old value of char array?

I was trying to test named pipe when i encountered this error. 遇到此错误时,我正在尝试测试命名管道。 I have a file named client.c who writes to a named pipe. 我有一个名为client.c的文件,它会写入命名管道。 And I have a file named server.c who reads from a named pipe and print its value. 我有一个名为server.c的文件,该文件从命名管道读取并打印其值。 Looks like there is some issue with server.c 看起来server.c有问题

Client code. 客户代码。

//-------CLIENT-----------
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define FIFO_FILE "MYFIFO"
int main()
{
    int fd, ch;
    char readbu[80];
    int write_byte;
    fd=open(FIFO_FILE,O_WRONLY);
    while(1)
    {
        printf("Enter choice \n1.Pepsi\n2.Coke\n3.Limca\n4.Maza\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: strcpy(readbu,"Pepsi\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 2: strcpy(readbu,"Coke\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 3: strcpy(readbu,"Limca\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 4: strcpy(readbu,"Maza\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            default:printf("Invalid");
            break;
        }
    }
    return 0;
}

Server code: 服务器代码:

// Server code    
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define FIFO_FILE "MYFIFO"
int main()
{
    int fd;
    char readbuf[80];
    char end[10];
    int to_end;
    int read_bytes;
    mknod(FIFO_FILE,S_IFIFO|0640,0);
    strcpy(end,"end");
    while(1)
    {
        fd=open(FIFO_FILE,O_RDONLY);
        read_bytes=read(fd,readbuf,sizeof(readbuf));
        readbuf[read_bytes]='\0';
        printf("Rec str = %s of len %d", readbuf,(int)strlen(readbuf));
        to_end=strcmp(readbuf,end);
        if(to_end==0)
        { close(fd);
            break;
        }`enter code here`
    }
    return 0;
}

At server side above code does not print any output. 在服务器端,上面的代码不打印任何输出。

If i change the printf statements to below then I observe that first iteration it prints blank line and then for other iteration it print old values. 如果我将printf语句更改为以下内容,那么我观察到该第一次迭代将打印空白行,然后在其他迭代中将打印旧值。

printf("\nRec str = %s of len %d", readbuf,(int)strlen(readbuf));

If i change the printf statements to below then I observe that it prints correct values. 如果我将printf语句更改为以下内容,那么我发现它会打印正确的值。

printf("Rec str = %s of len %d\n", readbuf,(int)strlen(readbuf));

I am completely confused how the \\n makes so much difference. 我完全感到\\ n有何不同。

I tried to check the value in readbuf using gdb and it actually contains the correct values. 我尝试使用gdb检查readbuf中的值,它实际上包含正确的值。 Kindly help me understand the trick here. 请帮助我理解这里的技巧。

The output buffer is not normally flushed until it is either full, a newline is inserted, or an explicit fflush( stdout ) call. 通常在输出缓冲区已满,插入换行符或显式fflush( stdout )调用之前,通常不会刷新输出缓冲区。

When you have the newline at the start of the output, it flushes the previously buffered data, and buffers the following data. 当在输出开始处有换行符时,它将刷新先前缓冲的数据,并缓冲后续数据。

The following will resolve the issue: 以下将解决该问题:

printf("\nRec str = %s of len %d", readbuf,(int)strlen(readbuf));
fflush( stdout ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM