Fgets (), gets (), eof, feof ()

Source: Internet
Author: User

1. fgets () and gets ()

1.1 char * fgets (char * STR, int num, file * stream );

// Reads a maximum of num characters from the stream to the character array 'str' and stops when a line break is encountered or when a num-1 character is read.

// Automatically append the '\ 0' NULL Character

1.2 char * gets (char * Str );

Read string function fgets

The function reads a string from a specified file to a character array.

The function is called in the form

Fgets (character array name n file pointer) where N is a positive integer. It indicates that the number of characters read from a file cannot exceed n-1.

After reading the last character, add the string ending sign '\ 0 '.

For example, fgets (STR, N, FP); reads n-1 characters from the file indicated by FP and sends them to the STR array.

// Read a string from the standard input stdin and terminate when a line break or end occurs.

// Unlike fgets, num is not specified, so pay attention to the STR size of the character array.

1. if you encounter a linefeed or EOF before reading n-1 characters, the reading is complete.

2. The fgets function also returns the first address of the character array.

Note: There is no macro-defined relationship between fgets and gets. Each of them has its own implementation. The implementation of the worm virus is the "credit" of the function gets ". The task of the gets function is to read a string from the stream. Its caller will tell it where to put the read string. However, the gets () function does not check the buffer size. If the caller provides a pointer to the stack and the number of characters read by the get () function exceeds the buffer space, get () will happily write the extra characters into the stack, which overwrites the original content in the stack.

For example, main ()

Char line [512];

// Allocate a space of 512 characters on the program stack...

Gets (line); // the entry of the worm virus, which can write malicious code into the stack through the extra data

}

1.3 fgetc ()

Read character function fgetc

The function of the fgetc function is to read a character function from a specified file in the form of a character variable = fgetc (File pointer)

For example, CH = fgetc (FP );

It means reading a character from the open file FP and sending it to Ch.

The usage of the fgetc function is described as follows:

1) The files read in the fgetc function call must be opened in read or read/write mode.

2) The result of reading characters can also be not assigned a value to the character variable.

3) There is a position pointer inside the file. It is used to point to the current read/write bytes of the file. When a file is opened, the pointer always points to the first byte of the file. After the fgetc function is used, the pointer will move one byte backward. Therefore, you can use the fgetc Function Multiple times to read multiple characters. Note that the file pointer is not the same as the position pointer inside the file. The file Pointer Points to the entire file, which must be defined in the program, as long as the value of the file pointer is not changed. The position pointer inside the file is used to indicate the current read/write position inside the file. Every read/write time, the pointer moves backward and does not need to be defined in the program, but is automatically set by the system.

1.4 write character function fputc

The fputc function is used to write a character function call from a specified file.

Fputc (character constants or variable files refer to quantities or variables)

For example, fputc ('A', FP). The usage of the putc function must also be described.

1) The written file can be opened in the write, read/write, or append mode. What is the write or read/write method ?? The write character starts from the beginning of the file. If you want to retain the words you want to write in the original file content ?? If the file to be written does not exist, the file is created.

2) each time a character file is written, the internal position pointer moves one byte backward.

3) The fputc function has a return value. If the write is successful, the written character is returned. Otherwise, an EOF is returned. This can be used to determine whether a writer writes a file and then reads the file content and displays it on the screen.

We recommend that you use the fgets function instead of the gets function. In addition, most of these get functions have corresponding put versions.

Int fputc (INT character, file * stream );

Int putc (INT character, file * stream); // implement int putchar (INT character) through macro definition and fputc );

// Implemented through macro definition:

# Define putchar (c) fputc (C, stdout)

Int fputs (const char * STR, file * stream );

Int puts (const char * Str );

Note: There is no macro-defined implementation relationship between the two. Puts (const char * Str) is approximately equivalent to fputs (cosnt char * STR, stdout). The difference is that the former also outputs a '\ n' last,

1.5 format the read/write functions fscanf and fprinf fscanf

The fprintf function is similar to the scanf and printf functions used earlier.

The difference between the fscanf function and fprintf function is that the Read and Write objects are not keyboard and display but disk files.

The call format of these two functions is

Fscanf (File pointer Format String input table)

Fprintf (File pointer Format String output table)

For example, fscanf (FP, "% d % s", & I, S); // write data from a file

Fprintf (FP, "% d % C", J, CH); // write

2.1 EOF

EOF is a symbolic constant defined in stdio. H files. The value is-1. For example, the fputc function returns a value: If the output is successful, the return value is the output character; If the output fails, an EOF is returned.

The fgetc function returns a file end mark (EOF) when reading characters. If you want to read characters from a disk file sequentially and display them on the screen,

Yes: CH = fgetc (FP); While (Ch! = EOF)

{

Putchar (CH );

Ch = fgetc (FP );

}

Note that EOF is not an output character and cannot be displayed on the screen. Since the ASCII code cannot contain-1, it is appropriate to define the EOF as-1.

When the read character value is equal to-1 (EOF), it indicates that the read character is not a normal character, but a file Terminator.

However, the above only applies to reading text files.

Currently, ansi c allows a buffer file system to process binary files. The value of binary data read from a certain byte may be-1, which is exactly the value of EOF. This results in the need to read useful data, but it is processed as "End of File ".

2.2 feof (FP)

It is used to test whether the current state of the file pointed to by FP is "End of File ".

To read data from a binary file in sequence, you can:

While (! Feof (FP ))

{

C = fgetc (FP );

...

}

The correct statement should be:
Char C;
C = fgetc (FP );
While (! Feof (FP ))
{
Printf ("% x/N", C );
C = fgetc (FP );
}

The reason is that after reading the last character, FP-> flag is still not set to _ ioeof, so feof () still does not detect the end of the file. Feof () cannot detect the end of a file until fgetc () is called again.

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.