File Operation summary, file Summary

Source: Internet
Author: User

File Operation summary, file Summary
1. open the file

Function Definition: FILE * fopen (char * pname, char * mode)

Function Description: Pname is the file name, and mode is the method for opening the file (r: Read-only, w: Write only)

Return Value: If NULL is returned, opening fails.

 

2. close the file

Function Definition: Int fclose (FILE * stream)

Function Description: Stream is the pointer to the FILE object.

Return Value: If 0 is returned, it is disabled successfully.

 

3. Read one character

Function Definition: Int fgetc (FILE * stream)

Function Description: Stream is the pointer to the FILE object.

Return Value: If EOF is returned, the read operation fails.

 

4. Write a character to the file

Function Definition: Int fputc (int char, FILE * stream)

Function Description: Char is the character to be written and transmitted using an integer. stream is the pointer to the FILE object.

Return Value: If EOF is returned, the write operation fails.

 

The following describes how to copy the content of one file to another.CodeAs follows:

1 /*
  2 file operations: copy the contents of one file to another
  3 * /
  4
  5 #include <stdio.h>
  6
  7 int main () {
  8 int ch;
  9 FILE * in, * out; // pointers to input and output files
10 // File failed to open
11 if ((in = fopen ("in.txt", "r")) == NULL || (out = fopen ("out.txt", "w")) == NULL) {
12 return;
13}
14 ch = fgetc (in); // enter a character
15 while (ch! = EOF) {
16 fputc (ch, out); // write a character
17 ch = fgetc (in); // continue typing
18}
19 fclose (in); // close the file stream
20 fclose (out);
twenty one 
22 return 0;
twenty three }

 

 

5. Read a string

Function Definition: Char * fgets (char * str, int n, FILE * stream)

Function Description: Str stores the input string. n indicates reading n-1 characters. stream is the pointer to the FILE object.

Return Value: If NULL is returned, no input is returned.

 

6. Write formatted data to the file

Function Definition: Int fprintf (FILE * stream, const char * format ,...)

Function Description: Similar to the printf function, stream is the pointer to the FILE object.

Return Value: If the write operation succeeds, the total number of written characters is returned. Otherwise, a negative number is returned.

 

7. Read formatted data

Function Definition: Int fscanf (FILE * stream, const char * format ,...)

Function Description: Similar to the scanf function, stream is the pointer to the FILE object.

Return Value: If successful, this function returns the number of successful matches and assignments. If the object ends or a read error occurs, EOF is returned.

 

The functions described above can be used to format input and output files,CodeAs follows:

1 /*
  2 File operations: formatted input and output of files
  3 * /
  4
  5 #include <stdio.h>
  6
  7 int main () {
  8 int id, age; // number, age
  9 char name [10]; // name
10 FILE * in, * out; // pointers to input and output files
11 // Failed to open the file
12 if ((in = fopen ("in.txt", "r")) == NULL || (out = fopen ("out.txt", "w")) == NULL) {
13 return;
14}
15 // Enter by format
16 while (fscanf (in, "% d% s% d", & id, name, & age)! = EOF) {
17 // output as format
18 fprintf (out, "id =% 04d, name =% s, age =% 02d \ n", id, name, age);
19}
20
21 return 0;
22 }

 

8. Determine the end Of the file

Function Definition: Int feof (FILE * stream)

Function Description: Stream is the pointer to the FILE object.

Return Value: When the end object identifier associated with the stream is set, this function returns a non-zero value. Otherwise, zero is returned.

TestCodeAs follows:

1 /*
  2 File operations: feof
  3 * /
  4
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <math.h>
  8 #include <stdlib.h>
  9 #include <time.h>
10 #include <stdbool.h>
11
12 int main () {
13 char str [512];
14 FILE * in;
15 // File failed to open
16 if ((in = fopen ("in.txt", "r")) == NULL) {
17 return;
18}
19 while (! Feof (in)) {
20 fgets (str, 512, in); // enter a whole line
21 printf ("% s", str); // output
twenty two     }
twenty three 
24 return 0;
25} 

 


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.