Standard C Episode 12

Source: Internet
Author: User

File structure used to record information about files
A pointer variable of the file struct type is called a document pointer.
The fopen function can open a file and make a variable of the corresponding file structure, using the address of the struct variable as the return value. Returns null if execution fails.

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7P_file = fopen ("a.txt","W");8     if(p_file) {9         //File Manipulation StatementsTen fclose (p_file); OneP_file =NULL; A     } -     return 0; -}

You need to specify how the file is to be used when using the fopen function. The mode of operation consists of three parts, respectively, as follows
The first part has three optional characters ' R ', ' W ' and ' a '. This part must be specified and only one character can be specified.
' R ' to read the file
' W ' to write to the file. Create a new text if the file does not exist
Otherwise remove the original file contents.
' A ' writes to the file. If the file already exists, the new internal
Append at end of file

The second part has an optional character ' + ' and this part is dispensable. If you use this section, you are reading and writing to the file.

The third part has an optional character ' B ', which is dispensable. If you use this section, it means that you want to do a binary operation on the file.

The Fclose function is used to close a file, and you must use this function to close a file when it is no longer in use.

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7     CharStr[] ="abcdef";8     intloop =0;9P_file = fopen ("a.txt","W");Ten     if(p_file) { One         //File Manipulation Statements A          while(Str[loop]) { -             if(EOF = =FPUTC (Str[loop], p_file)) { -                  Break; the             } -loop++; -         } - fclose (p_file); +P_file =NULL; -     } +     return 0; A}

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7P_file = fopen ("a.txt","R");8     if(p_file) {9         //File Manipulation StatementsTen         CharCH =0; One          Do { ACH =fgetc (p_file); -             if(EOF! =ch) { -printf"%c", ch); the             } -} while(EOF! =ch); -printf"\ n"); - fclose (p_file); +P_file =NULL; -     } +     return 0; A}

The FPUTC function can write a character to a file if it fails to return EOF.

The FGETC function can read a character from a file and return it if it exceeds the end of the file and returns EOF.

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7     intValues[] = {1,2,3,4,5};8P_file = fopen ("A.bin","WB");9     if(p_file) {Ten         //File Manipulation Statements One         intnum = fwrite (values,sizeof(int),5, p_file); Aprintf"total%d integer variable \ n", num); - fclose (p_file); -P_file =NULL; the     } -     return 0; -}

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7     intvalues[5] = {0}, loop =0;8P_file = fopen ("A.bin","RB");9     if(p_file) {Ten         //File Manipulation Statements One         intnum = Fread (values,sizeof(int),5, p_file); Aprintf"Total%d integer variables read \ n", num); -          for(Loop =0; Loop <=4; loop++) { -printf"%d", Values[loop]); the         } -printf"\ n"); - fclose (p_file); -P_file =NULL; +     } -     return 0; +}

The fwrite function can put multiple variables in the array at once in binary mode (binary write on Unix system and text write no difference, if written as text on the Windows system will write a newline character \ r \ n, accounting for two bytes) to the file, The return value indicates the number of variables written.

The Fread function can read the values of multiple variables from a file in binary mode at once and record them in an array, and the return value represents the number of variables read in.




The location pointer is used in the file structure to record the next read and write operation of the file, and the position pointer moves each time the file is read and written.
The rewind function can move the position pointer to the beginning of the file
The Fseek function can move the position pointer anywhere
Seek_set 0 represents the beginning of the file
Seek_cur 1 position pointer current position

Seek_end 2 represents end of file

1 /*2 Documentation Exercises3 */4#include <stdio.h>5 intMain () {6FILE *p_file =NULL;7P_file = fopen ("a.txt","R");8     if(p_file) {9         //File Manipulation StatementsTenFseek (P_file,2, seek_set); Oneprintf"%c", Fgetc (P_file)); A         //Rewind (p_file); -Fseek (P_file,1, seek_cur); -printf"%c", Fgetc (P_file)); the         //Rewind (p_file); -Fseek (P_file,-3, seek_end); -printf"%c", Fgetc (P_file)); -printf"\ n"); + fclose (p_file); -P_file =NULL; +     } A     return 0; at}


The fputs function can write a string to a file.

The Fgets function can read a string from a file.


The fprintf function can write data to any file in the specified format.
The FSCANF function can read data into a variable from any file in the specified format.

Standard C Episode 12

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.