Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Operation examples of C language Files

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of the operation example analysis of the C language file, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading the operation example analysis article of this C language file. Let's take a look.

First, why to use files

When we write some projects, we should store the written data. Only when we choose to delete the data, the data no longer exists. This involves the problem of data persistence, for us, the general methods of data persistence are to store the data in disk files, store data in the database and so on. Using files, we can store the data directly on the computer's hard disk, so that the data can be persisted.

What is a document

The files on disk are files.

In programming, we generally say that there are two kinds of files: program files and data files.

1. Program file

Includes source program files (suffix .c), destination files (windows environment suffix .obj), and executable programs (windows environment suffix .exe).

two。 Data file

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 the data, or the file from which the content is output.

3. File name

A file should have a unique file identity so that users can identify and reference it.

Composition of file name: file path + file name trunk + file suffix

For example: C:\ cyuyan\ test.txt

III. Document pointer

Each used file opens up a corresponding file information area in memory, which is used to store information about the file (file name, file status and current location of the file, etc.). This information is stored in a structure variable. The structure is declared by the system and named FILE.

The following file type declarations are found in the stdioh header file provided by the VS2013 compilation environment:

Struct _ iobuf {char * _ ptr; int _ cnt; char * _ base; int _ flag; int _ file; int _ charbuf; int _ bufsiz; char * _ tmpfname;}; typedef struct _ iobuf FILE

The FILE types of different C compilers contain different contents, more or less the same.

Create a pointer variable for FILE*:

FILE* pf;// file pointer variable

The defined pf is a pointer variable to FILE type data. You can make pf point to the file information area (structure variable) of a file. The file can be accessed through the information in the information area of the file. That is, the file associated with it can be found through the file pointer variable.

IV. Opening and closing of files

When writing a program, when you open a file, you will return a pointer variable of FILE* pointing to the file, which is equivalent to establishing a relationship between the pointer and the file.

Fopen-Open Fil

Fclose-close the file (be sure to close the file after use to prevent data loss)

/ / Open the file FILE * fopen (const char * filename, const char * mode); / / filename-to open the file name mode-file access mode / / close the file int fclose (FILE * stream)

Common file access modes are as follows:

For example:

# include#includeint main () {/ / Open file FILE* pf = fopen ("data.txt", "w"); if (pf = = NULL) {printf ("% s\ n", strerror (errno)); return;} / use file / / close file fclose (pf) Pf = NULL; return 0;} V. Sequential reading and writing of files

Function description:

Fgetc-reads a character int fgetc (FILE * stream) from the stream; stream-pointer to FILE structure fputc-writes characters to stream int fputc (int c, FILE * stream); c-character to be written stream-pointer to FILE structure

Fgets-get a string char * fgets (char * string, int n, FILE * stream) from the stream; string-where the data is stored n-maximum number of characters to read stream-pointer to the FILE structure fputs-write the string to the stream int fputs (const char * string, FILE * stream); string-output string stream-pointer to FILE structure

Fscanf-read formatted data from the stream

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

Stream-pointer to the FILE structure format-format control string argument-optional parameter

Fprintf-print formatted data to a stream

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

Stream-pointer to the FILE structure format-format control string argument-optional parameter

Fread-read binary data from a file

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

Buffer-where the data is stored size-item size (in bytes) count-maximum number of items to read stream-pointer to FILE structure

Fwrite-writes data to the stream in binary form

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

Buffer-where the data is stored size-item size (in bytes) count-maximum number of items to read stream-pointer to FILE structure

Scanf-A function that formats input from a standard input stream (stdin)

Printf-A function that formats output to a standard output stream (stdout)

Fscanf-you can read formatted data from a standard input stream (stdin) / specified file stream

Fprintf-output data in a formatted manner to a standard output stream (stdout) / specified file stream

Sscanf-you can extract (convert) formatted data from a string

Sprintf-converts a formatted data into a string

6. Fseek for random reading and writing of files

Locate the file pointer according to its location and offset

Int fseek (FILE * stream, long offset, int origin); / / move the file pointer to the specified location stream-pointer to the FILE structure offset-start bytes origin-start position

The use of fseek

Ftell

Returns the offset of the file pointer from the starting position

Long ftell (FILE * stream)

Use of ftell:

Rewind

Return the position of the file pointer to the starting position of the file

Void rewind (FILE * stream)

VII. Determination of the end of the document

Feof function:

Note: during the file reading process, the return value of the feof function cannot be used directly to determine whether the file is finished or not. Instead, it is used to determine whether the read fails or encounters the end of the file when the reading of the file ends.

1. Whether the reading of the text file ends, and determines whether the return value is EOF (fgetc) or NULL (fgets).

Fgetc determines whether the return value is EOF.

Fgets determines whether the return value is NULL.

two。 The reading end of the binary file is judged to determine whether the return value is less than the actual number to be read.

Fread determines whether the return value is less than the actual number to read.

Examples of text files:

Examples of binaries:

This is the end of the article on "operational example Analysis of C language Files". Thank you for your reading! I believe you all have a certain understanding of the knowledge of "operational example Analysis of C language documents". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 284

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report