Latest web development tutorials

C document literacy

Previous chapter we explained the standard input and output devices C language processing. In this chapter we will introduce the C programmer how to create, open, close, text or binary files.

A file, whether it is text or binary data, are representative of a series of bytes. C language function not only provides access to the top level, but also provides the underlying (OS) called to process files stored on the device. This chapter will explain the importance of the call the file manager.

open a file

You can use thefopen () function to create a new document or open an existing file, the call will initialize an object of type FILE,FILE type contains all the information necessary to control the flow. Here is the prototype of this call:

FILE *fopen( const char * filename, const char * mode );

Here, filename is a string used to name the file access mode modevalue can be one of the following values:

模式描述
r打开一个已有的文本文件,允许读取文件。
w打开一个文本文件,允许写入文件。如果文件不存在,则会创建一个新文件。在这里,您的程序会从文件的开头写入内容。
a打开一个文本文件,以追加模式写入文件。如果文件不存在,则会创建一个新文件。在这里,您的程序会在已有的文件内容中追加内容。
r+打开一个文本文件,允许读写文件。
w+打开一个文本文件,允许读写文件。如果文件已存在,则文件会被截断为零长度,如果文件不存在,则会创建一个新文件。
a+打开一个文本文件,允许读写文件。如果文件不存在,则会创建一个新文件。读取会从文件的开头开始,写入则只能是追加模式。

If the process is a binary file, you need to use the following access mode access mode to replace the above:

"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

Close the file

;

To close a file, use fclose () function. Function prototype is as follows:

 int fclose( FILE *fp );

If successful, close thefile, fclose () function returns zero, an error occurs when closing the file, the function returns EOF.This function is in fact, will clear the data in the buffer, close the file, and releases all memory for the file. EOF is a constant defined in the header filestdio.h in.

C standard library provides a variety of functions to press a character or in the form of fixed-length string to read and write files.

Write to file

Here is the write characters to a stream of the most simple function:

int fputc( int c, FILE *fp );

Functionfputc () writes the character c to the value of the parameter fp points to the output stream.If the write is successful, it returns the character written, if an error occurs, it returnsEOF.You can use the following functions to a null-terminated string to be written to the stream:

int fputs( const char *s, FILE *fp );

Functionfputs () writes the string sto fp points to the output stream. If the write is successful, it will return a non-negative value if an error occurs, it returnsEOF.You can also useint fprintf (FILE * fp, const char * format, ...) function to write a string to be written to a file.Try the following examples:

Note: Make sure you have available the / tmpdirectory, if this directory does not exist, you'll need to create the directory on your computer.

#include <stdio.h>

main()
{
   FILE *fp;

   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 will be created in the / tmp directory, a new filetest.txt, and use two different functions written two lines.Let us read the file.

Read the file

Here is the simplest function to read a single character from a file:

int fgetc( FILE * fp );

fgetc () function reads a character from the input file pointed to by fp.The return value is a character read, if an error occurs returnsEOF.The following function allows you to read a string from the stream:

char *fgets( char *buf, int n, FILE *fp );

Functionfgets () reads n from the input stream pointed to by fp - the one character.It will read the string copied into the bufferbuf, and finally appends a nullcharacter to terminate the string.

If the function before reading the last character encountered a newline character '\ n' at the end of the file or EOF, only to return to read the characters, including newline. You can also useint fscanf (FILE * fp, const char * format, ...) function to read a string from a file, but in the face of the first space character, it will stop reading.

#include <stdio.h>

main()
{
   FILE *fp;
   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 will read files created on a part, produces the following results:

1 : This
2: is testing for fprintf...

3: This is testing for fputs...

First, fscanf () method only read This,because it encounters a space in the back. Next, call thefgets () to read the rest, until the end of the line.Finally, callfgets () read in its entirety the second line.

Binary I / O functions

The following two functions for binary inputs and outputs:

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);

These two functions are used to read and write memory block - usually an array or a structure.