[英]C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?
[英]Why can't I printf() to stdout in C without using fflush()?
我最近開始用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)函數,以便將打印函數打印出到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
您能否詳細解釋為什么會突然出現差異?
fflush
的函數原型是這樣的:
int fflush ( FILE * stream );
它將文件指針刷新到流,以確保其已寫入。
根據執行代碼的環境,這種情況下的stdout
可能會被緩沖,這意味着它不會立即被寫入。 fflush
緩解了這種情況,並確保將其沖洗掉。
另一件事是,內核可能在執行時處於負擔之下,因此在這種情況下會延遲向控制台或終端的打印,在這種情況下,最終會明智地在整個位置fflush
。
圍繞OP的問題,將SCCE示例包含在內可能會有所幫助,很難區分原因,更多的是發生了什么。
編輯:
該代碼可以通過包含以下代碼段來指定在不進行緩沖的情況下自動寫入輸出
setbuf(stdout, NULL);
好的做法是,在開始時保存緩沖區控制的狀態,關閉緩沖區,然后在代碼執行結束時恢復緩沖區控制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.