PHP Text Manipulation Method Set comparison 1th/2 page _php tips

Source: Internet
Author: User
Tags fread rewind
string read and Write functions fgets and fputs

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.


Data block read-write function fread and fwrite

The C language also provides a read-write function for the entire block of data. Can be used to read and write a set of data, such as an array element, the value of a struct variable, and so on. The general form of the Read block function call is: Fread (BUFFER,SIZE,COUNT,FP); The general form of the Write block function call is: fwrite (BUFFER,SIZE,COUNT,FP); Where buffer is a pointer, in the Fread function, it represents the first address that holds the input data. In the Fwrite function, it represents the first address that holds the output data. The size represents the number of bytes in the data block. Count represents the number of blocks of data blocks to read and write. FP represents a file pointer.

For example:

Fread (FA,4,5,FP); The significance of this is that from the file referred to in FP, 4 bytes (a real number) are fed into the FA of the real group each time, reading 5 consecutive times, i.e. reading 5 real numbers into the FA.

[Example 10.6] Enter two student data from the keyboard, write a file, and read the data from 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%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);
}

This example program defines a structure Stu, illustrates two structure arrays Boya and BOYB as well as two structure pointer variables pp and QQ. PP points to boya,qq pointing to Boyb. The 16th line of the program opens the binary file "Stu_list" in read-write mode, enters two student data, writes to the file, then moves the file's internal position pointer to the top of the file, reads two student data, and displays it on the screen.
Current 1/2 page 12 Next read the full text

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.