The basic course of C language (iii) input and output functions and control flow statements (9)

Source: Internet
Author: User
Tags integer printf
Ii. functions related to file operations
The file read and write functions described in this section refer to sequential reading and writing, which means that the pointer automatically adds 1 after reading and writing a message. The following is a description of the write operation function and the read operation function respectively.
1. The sequential write function of the file
fprintf (), fputs (), and FPUTC () functions
Functions fprintf (), fputs (), and FPUTC () are the sequential write functions of the file, and the calling format is as follows:
int fprintf (FILE *stream, Char *format, <variable-list>);
int fputs (char *string, FILE *steam);
int FPUTC (int ch, FILE *steam);
The return values of the above three functions are integral types. The return value of the fprintf () function is the number of characters (in bytes) that are actually written to the file. If write error, return a negative number, the fputs () function returns 0 o'clock indicates that the operation of the string pointer to the file was successful, and returned not 0 o'clock to indicate that the write operation failed. The FPUTC () function returns a value that writes to the file, at which point the write operation succeeds, otherwise the EOF (defined in stdio.h) for the completion of the end of the file indicates a write error.
The formatting in the fprintf () function is the same as the printf () function, except that the fprintf () function is written to the file. and printf () is output to the screen.
Below is an example of running a Test.dat file after childbirth.
Example 11:
#include <stdio.h>
Main ()
{
Char *s= "That ' s good news"); /* Define string pointer and initialize/*
int i=617; /* Define integer variable and initialize/*
FILE *FP; /* Definition file pointer/*
Fp=fopne ("Test.dat", "w"); /* Create a text file only write * *
Fputs ("Your score of Toeflis", FP); * Write a string of characters to the created file/*
FPUTC (': ', FP); /* Write a colon to the file: * *
fprintf (FP, "%d\n", I); /* Write an integer number to the file you have created/*
fprintf (FP, "%s", s); /* Write a string to the created file/*
Fclose (FP); /* Close File/*
}
The DOS type command displays the contents of the TEST.DAT as follows:
Screen display
Your score of TOEFL is:617
That ' s good news
2. The sequential read operation function of the file
FSCANF (), fgets (), and fgetc () functions
The function fscanf (), fgets (), and fgetc () are the sequential read operations functions of the file, and the calling format is as follows:
int fscanf (FILE *stream, Char *format, <address-list>);
Char fgets (char *string, int n, FILE *steam);
int fgetc (FILE *steam);
The use of the FSCANF () function is similar to the scanf () function, except that it reads information from a file. The return value of the fscanf () function is EOF (that is,-1), indicating a read error, otherwise the read data is successful. The fgets () function reads up to n-1 characters from a file (n is used to specify the number of characters) and puts them in the string pointed to by a string, automatically adding a null character to the end of the string after it is read, and reading succeeds returning a string pointer, which fails to return a null pointer. The FGETC () function returns a character at the current position of the file, which returns EOF when reading an error.
The following program reads the Test.dat file produced by example 11 and displays the results of the readout on the screen.
Example 12
#include <stdio.h>
Main ()
{
Char *s, m[20];
int i;
FILE *FP;
Fp=fopen ("Test.dat", "R"); /* Open Text file Read Only * *
Fgets (S, FP); /* Read 23 characters from the file/*
printf ("%s", s); /* Output the Read string * *
FSCANF (FP, "%d", &i); /* Read integer number * *
printf ("%d", I); /* Output The number of read integer * *
Putchar (FGETC (FP)); /* Read a character at the same time output * *
Fgets (M, FP); /* Read 16 characters/*
Puts (m); /* Output Read String * *
Fclose (FP); /* Close File/*
Getch (); * * Wait for any key
}
After running screen display:
Your score of TOEFL is:617
That ' s good news
The same result can be obtained if FSCANF (FP, "%d", &i) in the previous example is changed to FSCANF (FP, "%s", m), and then the subsequent output statement is changed to printf ("%s", m). This shows Turbo C2. If you are reading a text file in 0, both the character and the number will be processed according to its ASCII value. The other thing to note is that the fscanf () function will automatically end when it reads a blank character, especially when used.

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.