stdio

C freopen descriptor - close manually/leave opened

岁酱吖の 提交于 2021-02-19 06:01:33
问题 I've been using freopen (from stdio.h) function before without asking myself this question. But now I'm unsure. E.g. I've reopened stdout: #define OUTPUT_FILE "out.txt" if ( freopen(OUTPUT_FILE,"w+",stdout) == NULL) printf("logout can't be opened\n"); Usually every process has and handles stdout, stderr, stdin automatically and we needn't care about closing of them. But how is it to be here? I have reopened stdout. Should I call fclose for closing reopened stdout? PS I'd be gladder to look at

Where to find struct _IO_FILE

吃可爱长大的小学妹 提交于 2021-02-06 02:39:00
问题 I was looking through /usr/include/stdio.h and happened to come across the following piece of code. /* Standard streams. */ extern struct _IO_FILE *stdin; /* Standard input stream. */ extern struct _IO_FILE *stdout; /* Standard output stream. */ extern struct _IO_FILE *stderr; /* Standard error output stream. */ /* C89/C99 say they're macros. Make them happy. */ #define stdin stdin #define stdout stdout #define stderr stderr My question is, where is this stucture struct _IO_FILE declared, i

Where to find struct _IO_FILE

好久不见. 提交于 2021-02-06 02:36:30
问题 I was looking through /usr/include/stdio.h and happened to come across the following piece of code. /* Standard streams. */ extern struct _IO_FILE *stdin; /* Standard input stream. */ extern struct _IO_FILE *stdout; /* Standard output stream. */ extern struct _IO_FILE *stderr; /* Standard error output stream. */ /* C89/C99 say they're macros. Make them happy. */ #define stdin stdin #define stdout stdout #define stderr stderr My question is, where is this stucture struct _IO_FILE declared, i

Where to find struct _IO_FILE

若如初见. 提交于 2021-02-06 02:35:46
问题 I was looking through /usr/include/stdio.h and happened to come across the following piece of code. /* Standard streams. */ extern struct _IO_FILE *stdin; /* Standard input stream. */ extern struct _IO_FILE *stdout; /* Standard output stream. */ extern struct _IO_FILE *stderr; /* Standard error output stream. */ /* C89/C99 say they're macros. Make them happy. */ #define stdin stdin #define stdout stdout #define stderr stderr My question is, where is this stucture struct _IO_FILE declared, i

Where to find struct _IO_FILE

随声附和 提交于 2021-02-06 02:35:27
问题 I was looking through /usr/include/stdio.h and happened to come across the following piece of code. /* Standard streams. */ extern struct _IO_FILE *stdin; /* Standard input stream. */ extern struct _IO_FILE *stdout; /* Standard output stream. */ extern struct _IO_FILE *stderr; /* Standard error output stream. */ /* C89/C99 say they're macros. Make them happy. */ #define stdin stdin #define stdout stdout #define stderr stderr My question is, where is this stucture struct _IO_FILE declared, i

What is the difference between a pointer to a buffer and a pointer to a file?

丶灬走出姿态 提交于 2021-01-29 07:24:44
问题 In Chapter 22 of " C Programming: A Modern Approach ", the basics of the <stdio.h> header are explained. One detail that has me a little confused is the difference between a pointer to a buffer and a pointer to a file (denoted as FILE * ). Consider the following (through which the confusion is derived): fopen is prototyped as: FILE *fopen(const char * restrict filename, const char * restrict mode) . fflush is prototyped as int fflush (FILE *stream) . fflush is described as a function the

C standard library corner case

北战南征 提交于 2021-01-28 08:35:16
问题 Is the following program a valid C program? #include <stdio.h> int main() { fwrite("x", 1, 1, stderr); fflush(stderr); fgetc(stderr); fwrite("y", 1, 1, stderr); return 0; } Notice that I try to read from stderr. When I compile it in Visual C++ 2008, and run it, I get the following output: xy which makes sense. However, when I redirect stderr to a file ( test.exe 2> foo.txt ), I get a "Debug Assertion Failed" window with the message: "Inconsistent Stream Count. Flush between consecutive read

Detecting background operation

旧城冷巷雨未停 提交于 2021-01-27 07:46:32
问题 In C, what is the way to detect a program was called in "background mode" ? I have a program I would like to launch either interactively or in background. How can I detect I should not be reading from stdin and end in a "Stopped : tty input" state ? Should I test that stdin is closed ? How can I do that ? Edit : isatty seems like a good idea, but what happen if stdin is a pipe end, and not a tty ? 回答1: Use the tcgetpgrp() function on the file descriptor of the controlling terminal (e.g. STDIN

Detecting background operation

微笑、不失礼 提交于 2021-01-27 07:45:55
问题 In C, what is the way to detect a program was called in "background mode" ? I have a program I would like to launch either interactively or in background. How can I detect I should not be reading from stdin and end in a "Stopped : tty input" state ? Should I test that stdin is closed ? How can I do that ? Edit : isatty seems like a good idea, but what happen if stdin is a pipe end, and not a tty ? 回答1: Use the tcgetpgrp() function on the file descriptor of the controlling terminal (e.g. STDIN

Aligning Output Values in C

人盡茶涼 提交于 2021-01-21 10:12:35
问题 So I'm working on a program which needs to format output. The output is supposed to be aligned, and it does do so with small numbers: But then when I give big numbers, it no longer works: My code is really but here's the part that prints the main output: /* The following code prints out the data */ printf("\n\nStatistics: \n\n"); printf("Descrip\t\tNumber:\t\tTotal:\t\tAverage:\n\n"); printf("Normal\t\t%d\t\t%d\t\t%d\n\n",normal_counter,normal_total,normal_average); printf("Short\t\t%d\t\t%d