Comparison of PHP text operation methods on page 1/2

Source: Internet
Author: User
Tags rewind

String read/write functions fgets and fputs

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 end string 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 <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 );
}

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 exactly 10.1 characters.ProgramThe first 10 characters.

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 <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 );
}

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.

Data Block read/write functions fread and fwrite

The C language also provides read/write functions for the entire data block. It can be used to read and write a group of data, such as an array element and a value of a structure variable. The call Method for reading data block functions is fread (buffer, size, Count, FP). The call Method for writing data block functions is fwrite (buffer, size, count, FP); buffer is a pointer. In the fread function, it indicates the first address to store the input data. In the fwrite function, it indicates the first address of the output data. Size indicates the number of bytes of the data block. Count indicates the number of data blocks to read and write. FP indicates the file pointer.

For example:

Fread (FA, FP); it means that four bytes (a real number) are read each time from the file indicated by FP and sent to the real array FA for five consecutive reads, read 5 real numbers into fa.

[Example 10.6] input two student data from the keyboard, write them into a file, and read the data of the two students on the screen.

# Include <stdio. h>
Struct Stu
{
Char name [10];
Int num;
Int age;
Char ADDR [15];
} Boya [2], boyb [2], * PP, * QQ;
Main ()
{
File * FP;
Char ch;
Int I;
Pp = Boya;
Qq = boyb;
If (FP = fopen ("stu_list", "WB +") = NULL)
{
Printf ("cannot open file strike any key exit! ");
Getch ();
Exit (1 );
}
Printf ("\ ninput data \ n ");
For (I = 0; I <2; I ++, pp ++)
Scanf ("% S % d % s", PP-> name, & PP-> num, & PP-> Age, PP-> ADDR );
Pp = Boya;
Fwrite (PP, sizeof (struct Stu), 2, FP );
Rewind (FP );
Fread (QQ, sizeof (struct Stu), 2, FP );
Printf ("\ n \ nname \ tnumber age ADDR \ n ");
For (I = 0; I <2; I ++, QQ ++)
Printf ("% s \ t % 5d % 7D % s \ n", QQ-> name, QQ-> num, QQ-> Age, QQ-> ADDR );
Fclose (FP );
}

In this example, the program defines a structure Stu, indicating two arrays Boya and boyb, and two structure pointer variables PP and QQ. PP points to Boya, QQ points to boyb. Line 2 of the program opens the binary file "stu_list" in read/write mode, enters two student data, writes it to the file, and moves the internal position pointer of the file to the beginning of the file, after reading two pieces of student data, it is displayed on the screen.

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.