是GETC()定义它返回EOF之后? [英] Is getc() defined after it has returned EOF?
问题描述
我用 GETC();
在C锻炼; Tibial,并回头看我发现了一些奇怪的程序后。我以为,在命令行参数给出的文件包含至少一个字节。 (它调用 GETC();
连续两次不检查 EOF
尝试它在一个空文件,它之后。仍然工作顺利我的问题是:是 GETC()的行为;
文件指针一个已经耗尽(EOF已经达到,而不是复卷)未定义或将它总是不断地返回EOF?
我想我可能会扩大这个问题在C STL所有的I / O功能,请在你的答案也澄清这一点。
下面是code的程序。该计划应该剥去所有评论的C / C ++源文件(和它的作品完美)。
的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv []){
INT状态= 0; //状态:0 =正常,1 =字符串2 =在评论中块注释3 =
INT ignchar = 0; //字符数忽视
INT CUR,接下来的; //当前字符与下一个
FILE * FP; //输入文件 如果(的argc == 1){
fprintf中(标准错误,用法:%s的file.c中\\ n,argv的[0]);
返回1;
} 如果((FP = FOPEN(的argv [1],R))== NULL){
fprintf中(标准错误,打开文件时出错\\ n);
返回2;
} CUR = GETC(FP); //初始化CUR,假定该文件包含至少一个字节
而((NEXT = GETC(FP))!= EOF){
开关(下){
案件 '/':
如果(国家&安培;!&安培; CUR =='/'){
状态= 2; //启动评论
ignchar = 2; //不打印此也不下一个字符(//)
}否则如果(状态== 3安培;&安培; CUR =='*'){
状态= 0; //块注释结束
ignchar = 2; //不打印此也不下一个字符(* /)
}
打破;
案件 '*':
如果(国家&安培;!&安培; CUR =='/'){
状态= 3; //块注释开始
ignchar = 2; //不打印此也不下一个字符(/ *)
}
打破;
情况下的'\\ n':
如果(状态== 2){
状态= 0;
ignchar = 1; //不打印当前的字符(现仍然在评论)
}
打破;
案件 '':
如果(状态== 0){
状态= 1;
}否则如果(状态== 1){
状态= 0;
}
} 如果(国家< = 1&安培;&安培;!ignchar)的putchar(现);
如果(ignchar)ignchar--;
CUR =下一个;
} 返回0;
}
标准输入输出文件保持这种被设置在第一时间结束文件达到,只能通过调用重置EOF标志的clearerr
或执行一个成功的 fseek的
或退
。因此,一旦 GETC
收益 EOF
一次,它会回头率 EOF
,即使新的数据可用,除非您使用上述方法之一清除EOF标志。
一些不符合标准的实现可以立即做出新的可用数据。此行为是有害的,可以打破一致性的应用程序。
I use getc();
in a C excercise, and after looking back on the program I noticed something weird. I assumed that the file given on the command line arguments contains at least one byte. (It calls getc();
twice in a row without checking for EOF
. After trying it on an empty file it still worked smoothly. My question is: is the behaviour of getc();
on a file pointer that's been exhausted (EOF has been reached and not rewinded) undefined or will it always continue to return EOF?
I think I could expand this question to all the I/O functions in the C STL, please clarify this in your answer too.
Here is the code for the program. The program is supposed to strip a C/C++ source file from all comments (and it works perfectly).
#include <stdio.h>
int main(int argc, char *argv[]) {
int state = 0; // state: 0 = normal, 1 = in string, 2 = in comment, 3 = in block comment
int ignchar = 0; // number of characters to ignore
int cur, next; // current character and next one
FILE *fp; // input file
if (argc == 1) {
fprintf(stderr, "Usage: %s file.c\n", argv[0]);
return 1;
}
if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Error opening file.\n");
return 2;
}
cur = getc(fp); // initialise cur, assumes that the file contains at least one byte
while ((next = getc(fp)) != EOF) {
switch (next) {
case '/':
if (!state && cur == '/') {
state = 2; // start of comment
ignchar = 2; // don't print this nor next char (//)
} else if (state == 3 && cur == '*') {
state = 0; // end of block comment
ignchar = 2; // don't print this nor next char (*/)
}
break;
case '*':
if (!state && cur == '/') {
state = 3; // start of block comment
ignchar = 2; // don't print this nor next char (/*)
}
break;
case '\n':
if (state == 2) {
state = 0;
ignchar = 1; // don't print the current char (cur is still in comment)
}
break;
case '"':
if (state == 0) {
state = 1;
} else if (state == 1) {
state = 0;
}
}
if (state <= 1 && !ignchar) putchar(cur);
if (ignchar) ignchar--;
cur = next;
}
return 0;
}
Stdio files keep an "eof" flag that's set the first time end-of-file is reached and can only be reset by calling clearerr
or performing a successful fseek
or rewind
. Thus, once getc
returns EOF
once, it will keep returning EOF
, even if new data becomes available, unless you use one of the aforementioned methods for clearing the eof flag.
Some non-conformant implementations may immediately make new data available. This behavior is harmful and can break conformant applications.
这篇关于是GETC()定义它返回EOF之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!