【发布时间】: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