Home > Back-end >  Strange question about printf buffer
Strange question about printf buffer

Time:11-30

If stdout is redirected to the printf must call flout (stdout) to refresh buffer
For example,
 # include & lt; Sys/stat. H> 
#include
#include
#include
#include
#include

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
 # include & lt; Sys/stat. H> 
#include
#include
#include
#include
#include

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];

CodePudding user response:

refer to the second floor wen_eric response:
help top
moderators, embarrassed, I understand the fflush refresh buffer, but still don't understand why close the stdout with printf need not fflush

CodePudding user response:

reference paschen reply: 3/f
http://en.cppreference.com/w/c/io/fflush

Webmaster, I'm sorry, I understand the fflush refresh buffer, but still don't understand why shut stdout with printf need not fflush before

CodePudding user response:

reference 4 floor zhao4zhong1 response:
printf % and variables within 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];
thank you for reply, but I don't quite understand and where is the contact problem, I'm sorry, please give advice or comments

CodePudding user response:

refer to 6th floor u014735054 response:
Quote: refer to the third floor paschen response:

http://en.cppreference.com/w/c/io/fflush

Webmaster, I'm sorry, I understand the fflush refresh buffer, but still don't understand why close the stdout with printf need not fflush


Prinf function may refresh buffer

CodePudding user response:

refer to the eighth floor paschen response:
Quote: refer to the sixth floor u014735054 response:

Quote: refer to the third floor paschen response:

http://en.cppreference.com/w/c/io/fflush

Webmaster, I'm sorry, I understand the fflush refresh buffer, but still don't understand why close the stdout with printf need not fflush


Prinf function may refresh buffer
why two printf second refresh buffer, and when a printf, no refresh

CodePudding user response:

references 9 f u014735054 response:
Quote: refer to the eighth floor paschen response:

Quote: refer to the sixth floor u014735054 response:

Quote: refer to the third floor paschen response:

http://en.cppreference.com/w/c/io/fflush

Webmaster, I'm sorry, I understand the fflush refresh buffer, but still don't understand why close the stdout with printf need not fflush


Prinf function may refresh buffer
why two printf second refresh buffer, and when a printf, no refresh


The first time you're not haven't open the file

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related