Int main (void) { Int fd. Close (STDOUT_FILENO); Fd=open (" hello ", O_CREAT | O_RDWR, 0644); If (fd & lt; 0 { Perror (" open "); exit(0); } Printf (" Nice to meet you! \n"); The fflush (stdout);//if you don't call what fflush nice to meet you will not be written to the file Close (fd); return 0; }
But I have found the following problems, very strange, please reassure
Int main (void) { Int fd. Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - \ n ");//add the printf even if not flush (stdout); Will be written to the file normally, this is why Close (STDOUT_FILENO); Fd=open (" hello ", O_CREAT | O_RDWR, 0644); If (fd & lt; 0 { Perror (" open "); exit(0); } Printf (" Nice to meet you! \n"); //fflush (stdout); Close (fd); return 0; }
CodePudding user response:
# include & lt; Stdio. H>
Int main () { The FILE * FILE=freopen (" out. TXT ", "w", stdout); If (file) { Printf (" Nice to meet you! \n"); } }
. I seem to remember fflush (stdout); Is not a standard?
CodePudding user response:
Help the top
CodePudding user response:
http://en.cppreference.com/w/c/io/fflush
CodePudding user response:
The inside of the printf % and variable of one to one correspondence relation The scanf % and variables and the variables inside before add don't add & amp; One-to-one correspondence relationship Is the place that C code is very easy to get wrong, and generally don't compile error, So during the compilation of source code is worth specially carefully check it again and even many times before,
Behind every last without \ n printf plus fflush (stdout); In each don't want to receive buffer in front of the old content affected the scanf and rewind (stdin); In addition, please check the return values of the scanf
//in the future, please use Int c; The scanf (" % c ", & amp; C); //, is changed to Char s [2]; Int c; The scanf (" % 1 s ", s); C=s [0];