【问题标题】:Why can't I printf() to stdout in C without using fflush()?为什么不使用 fflush() 就不能在 C 中将 printf() 打印到标准输出?
【发布时间】:2015-10-02 20:03:14
【问题描述】:

我最近开始使用 C 进行编程,但我似乎无法理解标准输出打印到屏幕的方式。

当我在 main() 函数中添加所有语句并使用 printf() 函数时,一切正常,所有 printf() 语句都能够打印到标准输出。

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) 函数,以便打印函数打印到标准输出:

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

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

【问题讨论】:

  • 不要为return 使用括号,它们不是必需的,这会使return 看起来像是一个函数。此外,你需要展示更多。因为你刚刚发布的内容不可能是问题,'\n' 字符应该会导致缓冲区被刷新。
  • 你的意思是输出没有立即显示(没关系,标准输出可能被缓冲了。)还是根本没有显示 , 即使在程序退出后?
  • 在@iharob 之后,请不要在回复 cmets 时更正您的问题,除非它有助于澄清问题。在这种情况下,它不会。只需发布有问题的确切代码即可。
  • 它可能是特定于实现的。您使用什么操作系统、什么文件系统、什么 C 标准库、编译器和编译选项? stdout 是伪终端(参见tty demystified...)还是管道?
  • 感谢 cmets,我添加了额外的代码。 @WeatherVane 下次我会记住这一点。

标签: c


【解决方案1】:

fflush 的函数原型是这样的:

int fflush ( FILE * stream );

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

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

另一件事,内核可能在执行时处于负载状态,因此在这种情况下会延迟打印到控制台或终端,在这种情况下,最终会明智地将fflush 撒在各处。

附上一个 SCCE 示例来查看 OP 的问题可能会有所帮助,区分原因并不容易,更确切地说是正在发生什么。

编辑:

代码可以通过包含这个sn-p来指定自动写入输出而不缓冲

setbuf(stdout, NULL);

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

【讨论】:

  • 请注意,禁用缓冲可能会大大降低输出速度。大多数时候,缓冲很有用
  • @BasileStarynkevitch 确实,我把它留在那里是为了解释缓冲的奥秘,这绝对是一个可选的东西,但对于不耐烦的人...... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多