while((c = getc(file))!= EOF)循环不会停止执行 [英] While (( c = getc(file)) != EOF) loop won't stop executing

查看:200
本文介绍了while((c = getc(file))!= EOF)循环不会停止执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的while循环不起作用。代码工作正常没有它...代码的目的是在bin文件中找到一个秘密消息。所以我得到了代码来找到这些字母,但现在当我试图让它循环直到文件结束时,它不起作用。我是新的我做错了什么?

  main(){

FILE * message;
int i,start;
long int size;
char keep [1];

message = fopen(c:\\myFiles\\Message.dat,rb);

if(message == NULL){
printf(读取文件时出现问题\\\
);
exit(-1);
}

//前4个字节包含一个int,表示可以丢弃多少个后续字节
fread(& start,sizeof(int),1,message );
printf(%i \\\
,start); //前4个字节的数目是280
fseek(message,start,SEEK_CUR); // skip 280 bytes
keep [0] = fgetc(message); //获取下一个字符,保持
printf(%c,keep [0]); //打印字符

while((keep [0] = getc(message))!= EOF){
fread(& start,sizeof(int),1,message);
fseek(message,start,SEEK_CUR);
keep [0] = fgetc(message);
printf(%c,keep [0]);
}

fclose(message);

system(pause);
}

编辑:



在调试器中查看我的代码之后,它看起来像在while循环中的getc抛出了一切。我通过创建一个新的char函数来修复它,然后用以下代码替换代码:

  fread(& start,sizeof (int),1,message); 
fseek(message,start,SEEK_CUR);

while((letter = getc(message))!= EOF){
printf(%c,letter);
fread(& start,sizeof(int),1,message);
fseek(message,start,SEEK_CUR);
}

它现在就像一个魅力。任何更多的建议当然欢迎。感谢大家。

解决方案

来自 getc()的返回值及其亲属是一个 int ,而不是一个 char



如果您将 getc()的结果分配给 char ,则返回 EOF




  • 如果简单 char 是无符号的,那么EOF被转换为0xFF,0xFF!= EOF,所以循环永远不会终止。

  • 如果简单 char 那么EOF相当于一个有效的字符(在8859-1代码集中,这是ÿ,y-umlaut,U + 00FF,LATIN SMALL LETTER Y WITH DIAERESIS),你的循环可以提前终止。



鉴于您遇到的问题,我们可以暂时猜测您将 char 作为无符号类型。



getc()等返回一个 int 他们必须返回可以适合 cha的每一个可能的值r ,还有一个不同的值EOF。在C标准中,它说:


ISO / IEC 9899:2011§7.21.7.1 fgetc()函数



int fgetc(FILE * stream);



如果 stream 指向的输入流的文件末尾指示符未设置,
存在下一个字符, fgetc 函数将 $ unsigned char 转换为 int ...



如果流的文件结尾指示符已设置,或者流是否处于文件结尾,流的结束
文件指示符被设置,并且 fgetc 函数返回EOF。


function:它们被定义为像 fgetc()函数的行为,除了如果 getc()被实现为一个宏观,可能会采取自由文件流参数通常不被授予标准宏 - 具体来说,流参数表达式可能会被不止一次地进行评估,因此调用 getc()与副作用( getc(fp ++))是非常愚蠢的(但更改为 fgetc(),这将是安全的,但仍然偏心)。






在你的循环中,你可以使用:

  int c; 

while((c = getc(message))!= EOF){
keep [0] = c;

这将保留赋值给 keep [0] ;我不确定你真的需要它。



你应该检查其他调用 fgets() getc() fread()以确保您获得了您期望的输入。特别是在输入上,你实际上不能跳过这些支票。更快,而不是以后,会出现问题,如果您不是宗教地检查退货状态,您的代码可能会崩溃,或者简单地出错。


I can't figure out why my while loop won't work. The code works fine without it... The purpose of the code is to find a secret message in a bin file. So I got the code to find the letters, but now when I try to get it to loop until the end of the file, it doesn't work. I'm new at this. What am I doing wrong?

main(){

FILE* message;
int i, start;
long int size;
char keep[1];

message = fopen("c:\\myFiles\\Message.dat", "rb");

if(message == NULL){
    printf("There was a problem reading the file. \n");
    exit(-1);
}

//the first 4 bytes contain an int that tells how many subsequent bytes you can throw away
fread(&start, sizeof(int), 1, message);
printf("%i \n", start); //#of first 4 bytes was 280
fseek(message, start, SEEK_CUR); //skip 280 bytes
keep[0] = fgetc(message); //get next character, keep it
printf("%c", keep[0]); //print character

while( (keep[0] = getc(message)) != EOF) {
    fread(&start, sizeof(int), 1, message);
    fseek(message, start, SEEK_CUR);
    keep[0] = fgetc(message);
    printf("%c", keep[0]);
}

fclose(message);

system("pause");
}

EDIT:

After looking at my code in the debugger, it looks like having "getc" in the while loop threw everything off. I fixed it by creating a new char called letter, and then replacing my code with this:

fread(&start, sizeof(int), 1, message);
fseek(message, start, SEEK_CUR);

while( (letter = getc(message)) != EOF) {
    printf("%c", letter);
    fread(&start, sizeof(int), 1, message);
    fseek(message, start, SEEK_CUR);
}

It works like a charm now. Any more suggestions are certainly welcome. Thanks everyone.

解决方案

The return value from getc() and its relatives is an int, not a char.

If you assign the result of getc() to a char, one of two things happens when it returns EOF:

  • If plain char is unsigned, then EOF is converted to 0xFF, and 0xFF != EOF, so the loop never terminates.
  • If plain char is signed, then EOF is equivalent to a valid character (in the 8859-1 code set, that's ÿ, y-umlaut, U+00FF, LATIN SMALL LETTER Y WITH DIAERESIS), and your loop may terminate early.

Given the problem you face, we can tentatively guess you have plain char as an unsigned type.

The reason that getc() et al return an int is that they have to return every possible value that can fit in a char and also a distinct value, EOF. In the C standard, it says:

ISO/IEC 9899:2011 §7.21.7.1 The fgetc() function

int fgetc(FILE *stream);

If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the fgetc function obtains that character as an unsigned char converted to an int ...

If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of- file indicator for the stream is set and the fgetc function returns EOF.

Similar wording applies to the getc() function and the getchar() function: they are defined to behave like the fgetc() function except that if getc() is implemented as a macro, it may take liberties with the file stream argument that are not normally granted to standard macros — specifically, the stream argument expression may be evaluated more than once, so calling getc() with side-effects (getc(fp++)) is very silly (but change to fgetc() and it would be safe, but still eccentric).


In your loop, you could use:

int c;

while ((c = getc(message)) != EOF) {
    keep[0] = c;

This preserves the assignment to keep[0]; I'm not sure you truly need it.

You should be checking the other calls to fgets(), getc(), fread() to make sure you are getting what you expect as input. Especially on input, you cannot really afford to skip those checks. Sooner, rather than later, something will go wrong and if you aren't religiously checking the return statuses, your code is likely to crash, or simply 'go wrong'.

这篇关于while((c = getc(file))!= EOF)循环不会停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆