Functions Fgets and Fputs, Fread and Fwrite, fscanf and fprintf usage summary of __ functions

Source: Internet
Author: User
Tags fread

It's been a long time, not a series.


string Read and write functions fgets and fputs:

1, fgets () function:
Prototype Char *fgets (char *s, int n, FILE *stream);
read out before reading the n-1 (n default 1024) character from the stream, such as if a newline character or EOF is encountered. The parameter S is to receive the string and, if successful, returns the pointer of S, otherwise null is returned.
You can also enter strings from the screen under Linux C, for example: Fputs (Fgets (S,n,stdin), stdout);
2, Fputs () function:
Prototype int fputs (char *string, FILE *stream);
Send a string to a stream.


fopen () Opens the file, fclose () closes the file.

data block Read and write functions 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.



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.