!=EOF 和 !feof() 的差别

用 C 写了一个读文件的函数,读普通文本没有问题,可是当读取一些二进制文件时就出现问题了–函数总是没法把文件的所有内容读出来。

郁闷了两天,终于找到答案。

原程序:

void bFile2str(String fpath, String& rStr){
  FILE* infile;
  infile = fopen (fpath.c_str(), “rb”);
  if (infile == NULL) {
    MessageBox(NULL, “Fail to open file, please check your path …” , “Error” , MB_ICONWARNING);
    return;
  }
  char cTemp;
  int i=1;
  cTemp = fgetc(infile);
  while (cTemp != EOF) {
    rStr = rStr + ” “;
    rStr[i] = cTemp;
    cTemp = fgetc(infile);
    i ++;
  } 
  fclose(infile);
}

后来改成:

void bFile2str(String fpath, String& rStr){
  FILE* infile;
  infile = fopen (fpath.c_str(), “rb”);
  if (infile == NULL) {
    MessageBox(NULL, “Fail to open file, please check your path …” , “Error” , MB_ICONWARNING);
    return;
  }
  char cTemp;
  int i=1;
  cTemp = fgetc(infile);
  while (!feof(infile)) {
    rStr = rStr + ” “;
    rStr[i] = cTemp;
    cTemp = fgetc(infile);
    i ++;
  } 
  fclose(infile);
}

分析: 分析有误,请看评论。
EOF 是文件结束符,当 fgetc 读字符时遇到文件结束符时, 将返回一个文件结束标志 EOF(即-1)。如果是普通文本,当然可以用EOF来判断是否已到文件尾,但在二进制文件中可能包含多个 EOF 标志符,这时就会出现文件还没读完就被认为文件已经结束的情况。而 feof() 就解决了这个问题,它可以用来测试当前文件指针是否在文件尾,它和 EOF 的机理完全不同,当然会跳过中间的许多 EOF 字符。

— EOF —

《!=EOF 和 !feof() 的差别》有24个想法

  1. 根本没有什么文件结束符。
    如果有的话空文件不是还要占一个字节的结束符?
    EOF是什么?它就是-1。它不过是C例程里的一个常量,当碰到文件结尾时,就返回-1。

    问题的根本是你错误的认为磁盘的文件结尾还有一个字符专门表示文件的结束,呵呵,没有这个字符,你想啊,每读一个字符还要比较一下它是否等于“空想的文件结束符“,是不是太低效了点呢。那是如何判断文件是否已经结束的?中断、异常。

  2. Hi there are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and
    set up my own. Do you need any html coding knowledge to make
    your own blog? Any help would be greatly appreciated!

  3. I do consider all the ideas you’ve offered on your post. They’re really convincing and can certainly work. Nonetheless, the posts are very brief for starters. May just you please lengthen them a little from subsequent time? Thank you for the post.

  4. You really make it seem so easy together with your presentation but I find this matter to be actually one thing that I feel I’d by no means understand. It sort of feels too complicated and very extensive for me. I’m taking a look ahead in your next submit, I will attempt to get the hold of it!|

  5. I used to be very pleased to find this net-website.I wanted to tnx! in your time for this excellent read!! I positively having fun with every little bit of it and I have you bookmarked to take a look at new stuff you weblog put up.:go to our website: jericbas.com

  6. Eine Frau geht einkaufen ihre Einkaufsliste war: Brot Apfel Milch und weiter alles nur ein Stück.

  7. I have noticed that online vineoris education is getting preferred because accomplishing your college degree online has changed into a popular solution for many people. Numerous people have not really had a possible opportunity to attend a normal college or university nonetheless seek the improved earning possibilities and a better job that a Bachelor’s Degree gives. Still others might have a college degree in one field but would like to pursue one thing they now possess an interest in.

  8. Nice post. I was checking continuously this blog and I am impressed! Extremely useful information particularly the last part :) I care for such information much. I was looking for this certain info for a very long time. Thank you and best of luck.|

  9. I have discovered some important matters through your blog post post. One other subject I would like to say is that there are numerous games available on the market designed especially for preschool age youngsters. They consist of pattern acknowledgement, colors, dogs, and models. These often focus on familiarization in lieu of memorization. This helps to keep little ones occupied without experiencing like they are studying. Thanks

  10. Thanks a lot for giving everyone a very nice chance to read critical reviews from this web site. It really is so amazing and as well , jam-packed with fun for me personally and my office friends to visit your web site more than thrice weekly to read the new issues you will have. Of course, I am just always impressed concerning the good strategies served by you. Some 3 tips on this page are really the most effective I’ve had.

  11. Aus einem Urlaubsflirt wird ein geiles anal frauen dieser Bildergalerie räkelt sich eine geile Frau mit geilen auf dem Bett sexy Posen.Dieser Body ist ein abslouter ober Knaller, sowas sexy!Die geile Amateurin hat wirklich geile , die sie auf diesen geilen Sexfotos gekonnt zur Schau stellt.Porno for free, jetzt alle Videos downloaden.Leck meine Du Sau!

发表评论

电子邮件地址不会被公开。 必填项已用*标注