Understanding of stdin, stdout and stderr under linux

Insert picture description here

Source: WeChat Official Account "Programming Learning Base"

Understanding of stdin, stdout and stderr under linux

Often seen in linuxstdin,stdout和stderr, These 3 can be called TerminalStandard input(standard input),Standard output(Standard out) andStandard errorOutput (standard error).

At the same time, you can find the files in the /dev directory under Linux

➜  ~ ls -l /dev | grep std
lrwxrwxrwx  1 root root          15 Sep 23 00:29 stderr -> /proc/self/fd/2
lrwxrwxrwx  1 root root          15 Sep 23 00:29 stdin -> /proc/self/fd/0
lrwxrwxrwx  1 root root          15 Sep 23 00:29 stdout -> /proc/self/fd/1

At the same time, characters can be output to /etc/stdout

~ echo "hello stdout" >> /dev/stdout
hello stdout~ 

Standard input

In the C language, it is expressed as calling the scanf function to accept user input, that is, inputting content from the terminal device. You can also use fscanf to specify stdin to receive content. The file identifier for standard input is 0 .

#include<stdio.h>
#include<string.h>
int main()
{
    
    
    char buf[1024];
    //C语言下标准输入
    scanf("%s",buf);
    //UNIX下标准输入 stdin 0
    fscanf(stdin,"%s",buf);
    //read不建议使用 标准输入必须为0不能为stdin
    read(0,buf,strlen(buf));
    return 0;
}

Standard out

In the C language, it is shown as calling the printf function to output the content to the terminal. You can also use fprintf to output the content to the terminal. The file identifier for standard output is 1

#include<stdio.h>
#include<string.h>
int main()
{
    
    
    char buf[1024];
    //C语言下标准输入 输出
    scanf("%s",buf);
    printf("buf:%s\n",buf);
    //UNIX下标准输入 stdin 0
    fscanf(stdin,"%s",buf);
    fprintf(stdout,"buf:%s\n",buf);
	//read,write不建议使用 
    //标准输入必须为0不能为stdin 标准输出必须为1不能为stdout
    read(0,buf,strlen(buf));
    write(1,buf,strlen(buf));
    return 0;
}

Standard error

The standard error and standard output are both output to the terminal, and the file identifier of the standard error output is 2 .

#include<stdio.h>
#include<string.h>
int main()
{
    
    
    char buf[1024]="error";
    fprintf(stderr,"%s\n",buf);

    write(2,buf,strlen(buf));
    return 0;
}

The difference between fgets and fscanf

fgets prototype:

char *fgets(char *s, int size, FILE *stream);

Example:

#include<stdio.h>
int main()
{
    
    
    char buf[128];
    fgets(buf,sizeof(buf),stdin);
    printf("%s",buf);
    printf("end\n");
    return 0;
}
➜  Desktop ./main          
dasdsad sadas
dasdsad sadas
end

Conclusion: fgets can get the content of a line of input, including line breaks\n

fscanf prototype:

int fscanf(FILE *stream, const char *format, ...);

Example:

#include<stdio.h>
int main()
{
    
    
    char buf[128];
    fscanf(stdin,"%s",buf);
    printf("%s",buf);
    printf("end\n");
    return 0;
}
➜  Desktop ./main 
hello world
helloend

Conclusion: fscanf can only get a string of strings, and ends with a space, excluding newline characters

Guess you like

Origin blog.csdn.net/qq_44519484/article/details/108755695