How to Use fputs () fgets () in C language, fputsfgets

Source: Internet
Author: User
Tags rewind

How to Use fputs () fgets () in C language, fputsfgets
1. The fgets function of the string reading function reads a string from a specified file to a character array. The function is called in the form of 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. Add the string end sign '/0' after the last character '. For example, fgets (str, n, fp); reads n-1 characters from the file indicated by fp and sends them to the str array.

[Example 10.4] Read a 10-character string from the e10_1.c file.

# Include
Main ()
{
FILE * fp;
Char str [11];
If (fp = fopen ("e10_1.c", "rt") = NULL)
{
Printf ("Cannot open file strike any key exit! ");
Getch ();
Exit (1 );
}
Fgets (str, 11, fp );
Printf ("% s", str );
Fclose (fp );
}

In this example, a string array of 11 characters is defined. After the e101.c file is opened in the text file mode, 10 characters are read from it and sent to the str array, add '/0' to the last cell of the array, and then display the output str array on the screen. The output 10 characters are the first 10 characters in the sample 10.1 program.

Fgets functions are described as follows:

1. If a linefeed or EOF is encountered before reading n-1 characters, the reading is complete.

2. The fgets function also has a return value, whose return value is the first address of the character array.

Ii. Write the string function fputs

The fputs function is used to write a string to a specified file. The call form is fputs (string, file pointer). The string can be a String constant or a character array name, or pointer variable, for example:

Fputs ("abcd", fp );

The meaning is to write the string "abcd" into the file specified by fp. [Example 10.5] append a string to the string created in example 10.2.

# Include
Main ()
{
FILE * fp;
Char ch, st [20];
If (fp = fopen ("string", "at +") = NULL)
{
Printf ("Cannot open file strike any key exit! ");
Getch ();
Exit (1 );
}
Printf ("input a string:/n ");
Scanf ("% s", st );
Fputs (st, fp );
Rewind (fp );
Ch = fgetc (fp );
While (ch! = EOF)
{
Putchar (ch );
Ch = fgetc (fp );
}
Printf ("/n ");
Fclose (fp );
}

In this example, a string must be added at the end of the string file. Therefore, the string file is opened in the append-read-write mode in line 2 of the program. Enter a string and use the fputs function to write the string to the file. In the 15 lines of the program, use the rewind function to move the internal position pointer of the file to the beginning of the file. Then go to the loop to display all the content in the current file one by one.

Http://blog.csdn.net/xiaciping/article/details/1094138

Related Article

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.