C Language Learning Tutorials Tenth-documents (6)

Source: Internet
Author: User
Tags exit count fread printf rewind

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.

Formatted read and write functions fscanf and fprintf

The FSCANF function, which is similar to the function of the scanf and printf functions used earlier, is a formatted read-write function. The difference between the FSCANF function and the fprintf function is not the keyboard and the monitor, but the disk file. The invocation format of these two functions is: fscanf (file pointer, format string, Input table column), fprintf (file pointer, format string, output table column), for example:
FSCANF (FP, "%d%s", &i,s);
fprintf (FP, "%d%c", j,ch);
The fscanf and fprintf functions can also be used to complete the problem of example 10.6. The modified program is shown in example 10.7.
[Example 10.7]
#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;
for (i=0;i<2;i++,pp++)
fprintf (FP, "%s%d%d%s\n",pp->name,pp->num,pp->age,pp->
addr);
Rewind (FP);
for (i=0;i<2;i++,qq++)
FSCANF (FP, "%s%d%d%s\n", qq->name,&qq->num,&qq->age,qq->addr);
printf ("\n\nname\tnumber age addr\n");
Qq=boyb;
for (i=0;i<2;i++,qq++)
printf ("%s\t%5d%7d%s\n", Qq->name,qq->num, Qq->age,
QQ-&GT;ADDR);
Fclose (FP);
}
Compared with example 10.6, the FSCANF and fprintf functions in this program can read and write only one structure array element at a time, so a circular statement is used to read and write all the array elements. Also note that the pointer variable pp,qq because the loop changed their values, so they were given the first address of the array on the 25 and 32 lines of the program.

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.