C language file operation

Posted by stuartshields on Thu, 03 Feb 2022 15:22:45 +0100

preface

When we haven't learned file operation and write code like address book, the original data will be destroyed every time we run it. It will require a lot of physical labor next time. If we learn file operation management, it won't be so troublesome!

1, What is a file

Files on disk are files. But in programming, we generally talk about two kinds of files: program files and data files (classified from the perspective of file function).

1. Procedure documents

Including source program files (suffix. c), object files (suffix. obj in windows Environment), and executable programs (suffix. exe in windows Environment).

2. Data files

The content of the file is not necessarily the program, but the data read and written when the program is running, such as the file from which the program needs to read data or the file that outputs the content.

3. File name

A file should have a unique file ID so that users can identify and reference it.
The file name consists of three parts: file path + file name trunk + file suffix
For example: C: \ code \ test txt
For convenience, the file ID is often referred to as the file name.

2, Opening and closing of files

1. File pointer

In the buffered file system, the key concept is "file type pointer", which is referred to as "file pointer".
Each used file opens up a corresponding file information area in the memory to store the relevant information of the file (such as the name of the file)
Word, file status and current location of the file, etc.). This information is stored in a structure variable. The structure type is systematic
Declared, named FILE
For example, stdio.com provided by VS2013 compilation environment The H header file contains the following file type declarations:

The FILE types of different C compilers contain different contents, but they are similar.
Whenever a FILE is opened, the system will automatically create a variable of FILE structure according to the situation of the FILE and fill in the information in it,
Users don't have to care about details.
Generally, the variables of this FILE structure are maintained through a FILE pointer, which is more convenient to use.
Next, we can create a pointer variable of FILE *:

FILE* pf;//File pointer variable

Definition PF is a pointer variable pointing to data of type FILE. You can make pf point to the FILE information area of a FILE (which is a structure variable). The FILE can be accessed through the information in the FILE information area. That is, the FILE associated with it can be found through the FILE pointer variable. For example:

2. Opening and closing of files

The file should be opened before reading and writing, and closed after use.
When writing a program, when opening a FILE, a pointer variable of FILE * will be returned to point to the FILE, which is equivalent to establishing a pointer
Relationship between needle and document.
ANSIC stipulates that fopen function is used to open the file and fclose is used to close the file.

//Open file
FILE * fopen ( const char * filename, const char * mode );
//Close file
int fclose ( FILE * stream );

The opening method is as follows:

3, Sequential reading and writing of files


The following is a detailed analysis of the above functions

1.fgetc

int fgetc( FILE *stream );

Function: read a character from an input stream (such as stdin) and return its Classl value
For example:
be careful:
1. Return value: EOF (- 1) is returned if reading ends or an error is encountered.

2.fputc

int fputc( int c, FILE *stream );

Function: write a character into a stream
For example:

int main()
{
	FILE* pf = fopen("w.dat", "w");

	fputc('a', pf);
	fclose(pf);
	pf = NULL;
}


be careful:
1. Return value: each function returns the written character. If EOF (- 1) is returned, it means reading error!

3.fgets

char *fgets( char *string, int n, FILE *stream );

Function: get a string from the stream.
For example:
First transfer the content to the file

int main()
{
	char arr[] = "abc";
	FILE* pf = fopen("t.dat", "w");
	fputs(arr, pf);
	fclose(pf);
	pf = NULL;

}


be careful:
1. The second parameter of fgets represents the number of characters to be read, but careful friends may find that the parameter is 2, but only one character is read? Just because * * "\ 0" * * also takes up a position size!
2. Return value: the address of the first element of the string is returned after normal reading, and NULL is returned after file reading or reading error.

4.fputs

int fputs( const char *string, FILE *stream )

Function: write a string into a stream
For example:

int main()
{
	char arr[] = "abc";
	FILE* pf = fopen("t.dat", "w");
	fputs(arr, pf);
	fclose(pf);
	pf = NULL;

}

be careful:
1. Return value: normal reading returns a non negative number, and reading error returns EOF (- 1).

5.fscanf

We can simply compare scanf with fscanf

int fscanf( FILE *stream, const char *format [, argument ]... )
int scanf( const char *format [,argument]... );

From the above, it is not difficult to find that fscanf has one more parameter than scanf: FILE*stream, or the default FILE*stream of scanf is stdin (standard input stream) It is not difficult to master the function through categories.
Function: format and read data from the stream.
For example:
Save data to formatted file first

int main()
{
	int a = 10;
	double b = 9.0;
	char arr[] = "abc";
	FILE* pf = fopen("t.dat", "w");
	fprintf(pf, "%d %lf %s", a, b, arr);
	fclose(pf);
	pf = NULL;

}


Reformat and take out

int main()
{
	int a = 0;
	double b = 0.0;
	char arr[10];
	FILE* pf = fopen("t.dat", "r");
	fscanf(pf, "%d %lf %s", &a, &b, arr);
	fclose(pf);
	pf = NULL;

}

6.fprintf

int fprintf( FILE *stream, const char *format [, argument ]...)

Function: pass formatted data into the stream.
For example:

int main()
{
	int a = 10;
	double b = 9.0;
	char arr[] = "abc";
	FILE* pf = fopen("t.dat", "w");
	fprintf(pf, "%d %lf %s", a, b, arr);
	fclose(pf);
	pf = NULL;

}

7.fread

size_t fread( void *buffer, size_t size, size_t count, FILE *stream )

Function: read data from stream
For example:
Enter data into the file first

int main()
{
	char arr[] = "abcd";
	FILE* pf = fopen("a.dat", "w");
	fwrite(arr, sizeof(char), strlen(arr)+1, pf);
	fclose(pf);
	pf = NULL;

}

Re read data

int main()
{
	char arr[5];
	FILE* pf = fopen("a.dat", "r");
	fread(arr, sizeof(char), strlen(arr), pf);
	fclose(pf);
	pf = NULL;

}

be careful:
Fread returns the number of complete entries actually read. If an error occurs, or if the end of the file is encountered before reaching count, the return value may be less than count.

8.write

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

For example:

int main()
{
	char arr[] = "abcd";
	FILE* pf = fopen("a.dat", "w");
	fwrite(arr, sizeof(char), strlen(arr), pf);
	fclose(pf);
	pf = NULL;

summary

The above is the summary of C language file operation, which will be supplemented later.

Topics: C C++