C Language Learning Tutorials Tenth-documents (5)

Source: Internet
Author: User
Tags printf rewind

The function of reading string function Fgets function is to read a string from a specified file into a character array in the form of a function call: fgets (character array name, n, file pointer), where n is a positive integer. Indicates that a string read from a file does not exceed n-1 characters. After the last character read, add a string end sign '. For example: Fgets (STR,N,FP) is meant to read n-1 characters into the character array str from the file referred to in FP.
[Example 10.4] reads a string containing 10 characters from the e10_1.c file.
#include <stdio.h>
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);
}
This example defines a character array of 11 bytes, reads out 10 characters into the STR array after opening the file e101.c as a read text file, and then displays the output str array on the screen in the last cell of the array. The 10 characters of the output are the first 10 characters of the example 10.1 program.

There are two points to the Fgets function:
1. read out the end of a newline character or EOF before it is read out n-1.
2. The Fgets function also has a return value, whose return value is the first address of a character array.

Second, write String function fputs

The function of the fputs function is to write a string to the specified file, which is called: fputs (string, file pointer) where the string can be a string constant, or it can be a character array name, or a pointer variable, for example:
Fputs ("ABCD", FP);
The meaning is to write the string "ABCD" into the file referred to in FP. [Example 10.5] appends a string to the file string established in Example 10.2.
#include <stdio.h>
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);
}
This example requires that you write a string at the end of a string file, so you open the file string in the 6th line of the program by appending the read-write text file. The string is then entered, and the string is written to a file string using the Fputs function. On the program 15 line, use the rewind function to move the file's internal position pointer to the top of the file. Then go to the loop to display the entire contents of the current file one by one.

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.