Feof read string

Source: Internet
Author: User
Feof () is a function that is often used to operate files in C language. This function is used to indicate whether we have reached the next position at the end of the file. Both binary and text files work. Compared with EOF (generally defined as-1), EOF can only be used to determine whether a text file has reached the end, because numbers in the text file are represented in ASCII, and the value range of ASCII characters is 0 ~ 255. The binary file may contain-1, so EOF cannot be used to determine the end. One problem we often encounter when using feof is to use fgets to read files and print them with fputs. The last line is printed twice. Why? Let's talk about the feof () function in stdio. h contains the definition of feof: # DEFINE _ ioeof 0x0010 # define feof (_ stream)-> _ flag & _ ioeof) therefore, feof () returns 1 only when _ flag = _ ioeof. In VC, only when the file position indicator (FP-> _ PTR in Windows) reaches the end of the file, and then the read/write operation occurs, FP-> _ flag will be set to include _ ioeof, and then call feof () to get the end information of the file. It is not the file position indicator ended at the end of the file, and feof () considers the file to end. That is to say, feof () returns 1 only when the file pointer reaches the next position at the end of the file. The program is as follows: While (! Feof (FP) {fgets (STR, 80, FP); fputs (STR, stdout);} Then after we read the last row, print the last row. At this time, feof () 0 is returned. then use fgets to read the file. Because the fgets () function will judge whether _ ioeof is encountered when reading the file, if it is encountered at the beginning, the content of the transmitted array will not change, that is, the content of the last line. In this way, the last row is printed twice. The fgets () function is defined at the end of the article. So how can we solve this problem. How can we use feof () correctly? Remember one principle: Read the content first, and then use feof () to judge. In this way, if you read the file after the last row of the file, feof () returns 1. The modified program is as follows: fgets (STR, 80, FP); While (! Feof (FP) {fputs (STR, stdout); fgets (STR, 80, FP);} solution 2: Do not use feof (), use the return value of fgets () to judge. if it is null, the end of the file is encountered. Fgets (STR, 80, FP); While (! Feof (FP) {fputs (STR, stdout); fgets (STR, 80, FP);} can print the file content. Content in the fgets. c file, which should be defined by the fgets function. _ Tschar * _ cdecl _ fgetts (_ tschar * string, int count, file * Str) {reg1 file * stream; reg2 _ tschar * pointer = string; _ tschar * retval = string; int ch; _ validate_return (string! = NULL) | (COUNT = 0), einval, null); _ validate_return (count> = 0), einval, null); _ validate_return (STR! = NULL), einval, null); If (COUNT = 0) {return NULL;} stream = STR; _ lock_str (Stream ); _ Try {# ifndef _ Unicode _ validate_stream_ansi_setret (stream, einval, retval, null); # endif if (retval! = NULL) {While (-- count) {If (CH = _ fgettc_nolock (Stream) = _ teof) {If (pointer = string) {retval = NULL; goto done;} break;} If (* pointer ++ = (_ tschar) CH) = _ T ('\ n') break ;} * pointer = _ T ('\ 0');} done:;} _ finally {_ unlock_str (Stream);} return (retval);} http://blog.sina.com.cn/s/blog_5eb8ebcb0100qca5.html

 

Feof read string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.