Harry Potter and the deathly H PHP text operation method Set compare 1th 2 page

Source: Internet
Author: User
Tags fread rewind
string read and Write functions fgets and fputs
The function of reading a string function Fgets function is to read a string into a character array from the specified file, in the form of a function call: fgets (character array name, n, file pointer), where n is a positive integer. Indicates that a string that is read from a file does not exceed n-1 characters. After the last character read, add the string end flag ' \ s '. For example: Fgets (STR,N,FP), the meaning is to read from the file referred to in the FP n-1 characters into the character array str.
[Example 10.4] reads a string containing 10 characters 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);
}
This example defines a character array str total of 11 bytes, after opening the file e101.c as a read text file, read out 10 characters into the STR array, in the last cell of the array will be added ' \ ', and then display the output str array on the screen. The output of the 10 characters is exactly the first 10 characters of the example 10.1 program.
The Fgets function has a two-point description:
1. Before the n-1 characters are read out, if a newline character or EOF is encountered, the readout ends.
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 in the form of: 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 by the FP. [Example 10.5] appends a string to the file 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);
}
This example requires the string to be written at the end of the string file, so the file string is opened in line 6th of the program to append a read-write text file. It then enters a string and writes it to the file string using the Fputs function. In program 15, use the rewind function to move the file's internal position pointer to the top of the file. Then enter 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 read and write functions 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 a read block function call is: Fread (BUFFER,SIZE,COUNT,FP); The general form of a write block function call is: fwrite (BUFFER,SIZE,COUNT,FP); Where buffer is a pointer to the Fread function, which represents the first address that holds the input data. In the Fwrite function, it represents the first address that holds the output data. A size represents the number of bytes of data block. Count represents the number of block blocks of data to read and write. The FP represents a file pointer.
For example:
Fread (FA,4,5,FP); The meaning is that from the file referred to in the FP, each read 4 bytes (a real number) into the real group FA, read 5 consecutive times, that is, read 5 real numbers into FA.
[Example 10.6] input two student data from the keyboard, write a file, and then read the data of the two students displayed on the screen.
#include
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 arrays of structures Boya and BOYB, and two structure pointer variables pp and QQ. PP pointing boya,qq to Boyb. The program line 16th reads and writes the binary file "Stu_list", enters two student data, writes to the file, then moves the file's internal position pointer to the top of the file, and reads out two student data to display on the screen.

Current 1/2 Page 12 next page

The above describes the Harry Potter and the deathly H PHP text operation method Set comparison 1th/2 page, including Harry Potter and the deathly h aspect of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.