而((C = GETC(文件))!= EOF)循环将不会停止执行 [英] While (( c = getc(file)) != EOF) loop won't stop executing

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

问题描述

我不明白,为什么我的while循环将无法工作。在code正常工作离不开它......在code的目的是找到一个bin文件一个秘密消息。所以,我得到了code找字母,但现在当我试图得到它的循环,直到文件的末尾,这是行不通的。我是新这里。我在做什么错了?

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");
}

编辑:

看着在调试器我的code后,它看起来像在while循环中有GETC把一切关闭。我创建了一个名为信新字符,然后用这个代替我的code固定它:

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.

推荐答案

的返回值GETC()及其亲属是 INT ,而不是字符

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

如果您分配的结果GETC()字符,以下两种情况之一发生时,它返回 EOF

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


  • 如果纯字符是无符号的,则EOF被转化为0xFF,到0xFF!= EOF,所以循环永远不会终止。

  • 如果纯字符签订后,则EOF相当于一个有效的字符(在8859 code组,这是Y,Y变音符,U + 00FF,拉丁小写字母Y带分音符),和你的循环可能会提前终止。

  • 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.

之所以 GETC()等人返回 INT 的是,他们必须返回每一个可能的值可以适合在字符,也是一个不同的值,EOF。在C标准,它说:

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的龟etc()函数

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

INT龟etc(FILE *流);

如果输入流中的档案结尾指标由流指向未设置和
  下一个字符是present,在龟etc 函数获取字符作为 unsigned char型转换为 INT ...

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 ...

如果对于流的结束的文件指示符被设置,或者如果流是在文件结束-,最终OF-
  对于流文件指示器设置和龟etc 函数返回EOF。

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.

类似的措辞适用于 GETC()函数和的getchar()功能:它们定义的行为像龟etc()除了功能,如果 GETC()实现为一个宏,它可能会采取与自由文件通常不授予标准宏流参数 - 具体而言,流参数前pression可以被评估一次以上,因此与副作用(称之为龟etc(FP ++))是非常愚蠢的。

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 it with side-effects (fgetc(fp++)) is very silly.

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

In your loop, you could use:

int c;

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

这preserves分配到保持[0] ;我不知道你真正需要它。

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

您应检查以与fgets其他电话() GETC() FREAD(),以确保你得到你所期望的输入内容。特别是在输入时,你无法真正买得起跳过这些检查。越早,而不是以后,事情会出错,如果你没有宗教检查返回状态,你的code是有可能崩溃,或者干脆出错。

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'.

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

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