Describes how the C programmer creates, opens, and closes a text file, or a binary file.
A file,means a series of bytes, whether it is a text file or a binary file, C Language, not only provides the access to the top level functions, but also provides the underlying (OS) call to process files on the storage device. This chapter will explain the important calls in document management.
Usually using the fopen () function to create a new file or open an existing file, this call initializes an object of the type FILE that contains all the necessary information to control the flow. Here is the prototype of this function call:
FILE *fopen ( const char * filename , const char * mode );
Here filename is a string to name a file, the value of the access mode can be one of the following values:
pattern |
description |
r |
Open an existing text file that allows it to be read. |
w |
Open a text file that allows writing to the file. If the file does not exist, a new file is created. Here, your program writes the content from the beginning of the file. If the file exists, it will be truncated to zero length and re-written. |
a |
Open a text file and write to the file in an append mode. If the file does not exist, a new file is created. Here, your program appends content to the files you already have. |
r+ |
Open a text file that allows you to read and write the file. |
w+ |
Open a text file that allows you to read and write the file. If the file already exists, the file is truncated to zero length, and if the file does not exist, a new file is created. |
a+ |
Open a text file that allows you to read and write the file. If the file does not exist, a new file is created. The read starts at the beginning of the file, and the write is only in append mode. |
If processed binary file, use the following access mode to replace the above:
“rb”, “wb”, “ab”, “rb+”, “r+b”, “wb+”, “w+b”, “ab+”, “a+b”
To close the file, please use the fclose() function. The prototype of the function is as follows:
int fclose ( FILE *fp );
The C standard library provides various functions to read and write files by characters or as a fixed-length string.
Here are the simplest functions to write characters to the stream:
int fputc ( int c , FILE *fp );
The function fputc () writes the character value of the parameter c into the output stream that the fp points to. If the writes are successful, it returns the written character and the EOF if an error occurs. You can use the following function to write a string ending with a null to the stream:
int fputs ( const char *s , FILE *fp );
The function fputs () writes the string s to the output stream where the fp points to. If the writes succeed, it returns a non-negative value and the EOF if an error occurs. You can also use the function of int fprintf (FILE * fp, const char * format,…) writes a string to the file. Try the following example:
Note:Make sure that you have an available tmp directory, and if it does not exist, you need to create it on your computer first.
/ tmp is usually a temporary directory on the Linux system. If you run on the Windows system, you need to modify it to an existed directory in the local environment, such as: C: tmp, D: tmp, etc.
#include <stdio.h > int main () { FILE *fp = NULL ; fp = fopen (“/tmp/test.txt “, “w+”); fprintf (fp , “This is testing for fprintf…n “); fputs (“This is testing for fputs…n “, fp ); fclose (fp ); }
When the above code is compiled and executed, it creates a new file test.txt inthe / tmp directory. And writes to two lines using two different functions. Let’s read this file next.
The following is the simplest function to read a single character from a file:
int fgetc ( FILE * fp );
The fgetc () function reads a character from the input file to which the fp points. The return value is the read character and the EOF if an error occurs. The following function allows you to read a string from a stream:
char *fgets ( char *buf , int n , FILE *fp );
The function fgets () reads n-1 characters from the input stream directed by fp. It copies the read string to the buffer buf and appends a null character at the end to terminate the string.
If this function encounters a broken line character ’ n’ or the EOF of the end of file before reading the last character, then only returned to the read characters, including line breaks. You can also use the int fscanf (FILE * fp, const char * format,…) function to read the string from the file, but it stops reading when encountering the first space and line break.
#include <stdio.h > int main () { FILE *fp = NULL ; char buff [255]; fp = fopen (“/tmp/test.txt “, “r “); fscanf (fp , “%s “, buff ); printf (“1: %s n “, buff ); fgets (buff , 255, (FILE *)fp ); printf (“2: %s n “, buff ); fgets (buff , 255, (FILE *)fp ); printf (“3: %s n “, buff ); fclose (fp ); }
When the above code is compiled and executed, it reads the files created in the previous section, producing the following results:
1: This 2: is testing for fprintf…
3: This is testing for fputs…
First, the method of fscanf() only reads This .because it encounters a space in the back. Second, call functon fgets () to read the remaining part until the end of the line. Finally, call fgets () to read the second row completely.
The following two functions are used for the binary input and output:
size_t fread (void *ptr , size_t size_of_elements , size_t number_of_elements , FILE *a_file ); size_t fwrite (const void *ptr , size_t size_of_elements , size_t number_of_elements , FILE *a_file );
Both functions are read and write for storage blocks-usually arrays or structures.
Above about C file reading and writing belongs to HDV Phoelectron Technology Ltd., a software technical operation. And the company for network related equipment (such as: AC ONU / communication ONU / intelligent ONU / fiber ONU, etc.) has brought together a powerful software team, for every customer customize the exclusive demands who need it, also let our products more intelligent and advanced.