为什么我不能在不使用fflush()的情况下将printf()打印到C中的stdout?

时间:2015-10-02 20:03:15

标签: c

我最近开始用C编程,而我似乎无法理解的是stdout打印到屏幕的方式。

当我在main()函数中添加所有语句并使用printf()函数时,一切运行良好,所有printf()语句都能够打印到stdout。

From Main.c
...
#include "HeaderFile.h"
int main(int argc, char * argv[]){
 ...
 printf("%c\n", testChar);
 return 0;/*End of execution. Returns 0 value and ends gracefully*/
}
...

但是当我开始在不同的函数中模块化我的代码时,我意识到我必须在每个printf()函数的末尾插入fflush(stdout)函数,以便将print函数打印到stdout:< / p>

From ReadFile.c
...
#include "HeaderFile.h"
void readFileFunction(char* file){
 ...
 printf("%c\n", testChar);
 fflush(stdout);
 ...
}
...

标题文件:

 /*This is the header file used by the Linked list program.*/
/*This is the header file used by the Linked list program.*/
#ifndef HEADERFILE_H   /* Include guard */
#define HEADERFILE_H

#include <stdio.h> /*including the stdio file inside the Main.c file. stdio.h is a header file, where this and other similar functions are defined.*/
#include <string.h>/*including the string file inside the Main.c file. string.h is a header file, where this and other similar functions are defined.*/
#include <time.h>/*including the time file inside the Main.c file. time.h is a header file, where this and other similar functions are defined.*/
#include <stdint.h>/*including the stdint file inside the Main.c file. stdint.h is a header file, where this and other similar functions are defined.*/
#include <stdlib.h>/*including the stdlib file inside the Main.c file. stdlib.h is a header file, where this and other similar functions are defined.*/
#include <errno.h> /*including the errno file inside the Main.c file. errno.h is a header file, where this and other similar functions are defined.*/
#include <regex.h>

extern const char errorString[]; /*A string of characters. Indicates an error message when the program in-counters a problem during execution.*/   

/*String constants used to match user input with a specific function.*/
extern const char *string1;
extern const char *string2;
extern const char *string3;
extern const char *string4;
extern const char *string5;
extern const char *string6;
extern const char *string7;
extern const char *string8;
extern const char *string9;

/*Node structure with character value and the next node.*/
typedef struct node {
    char value;
    char type;
    struct node * next;
} nodeStruct;

/*function prototypes for every function being used in the code.*/
int removeChar(nodeStruct ** head, char value);
void readFileInit(char* file);
void readFileFunction(char *file);
void printList(nodeStruct * head) ;
void push(nodeStruct ** head, char value) ;
char tail(nodeStruct * head) ;
char head(nodeStruct * head) ;
int length(nodeStruct * head) ;
int pop(nodeStruct ** head) ;
int regularExpr (const char *patt, char *str) ;
void append(nodeStruct ** head, char value) ;
int insertAfter(nodeStruct ** head, char value, char value2) ;
int insertBefore(nodeStruct ** head, char value, char value2) ;

#endif // HEADERFILE_H

请您详细解释为什么会出现这种突然的差异?

1 个答案:

答案 0 :(得分:4)

fflush的函数原型是:

int fflush ( FILE * stream );

它将文件指针刷新到确保其写入的流。

根据执行代码的环境,在这种情况下stdout可能会被缓冲,这意味着它不会立即被写入。 fflush可以缓解这种情况,并确保将其清除。

另一方面,内核可能在执行点处受到负载,从而在这种情况下将打印延迟到控制台或终端,在这种情况下,最后明智地在整个地方洒上fflush。 / p>

在查看OP的问题时,附上SCCE示例可能会有所帮助,要区分原因并不容易,更多的是发生了什么。

修改

代码可以指定通过包含此代码段

自动写入输出而不进行缓冲
setbuf(stdout, NULL);

最好在开始时保存缓冲区控制状态,关闭缓冲区,并在代码执行结束时恢复缓冲区控制。