Common File Operations in C Language

Source: Internet
Author: User

Original article link

I often feel that it is good for me to use many things, so I don't have to solve it. For example, every time I encounter a file operation, I need to check related APIs and examples, and then write them down based on the examples. It may be because of this kind of attitude, which keeps me in a half bucket of water. After reading "C expert programming", I feel that many things contain a lot of knowledge. After opening a blog, I often want to write something useful, something like taking notes. Note-taking has the advantage of having to sort out existing knowledge before you can write it down in an organized manner. Written yesterdayAlgorithmI encountered file operations again. This time I simply sorted out all the related APIs, but I took a note.

 

Before operating a file, open the file first. After using the file, close the file. APIS related to file operations are as follows:

 

C Code
# Include <stdio. h> File * Fopen ( Const   Char * Path, Const   Char * Mode ); // Path indicates the file path, and mode indicates the file usage.  Int Fclose (File * PF ); //  Otherwise, EOF is returned and the error is recorded in the system global variable errno.  //  Reads a character from the current position of stream  Int GETC (File * Stream );  //  Write c to the current position of the stream. 0 is returned for success; otherwise, EOF is returned.  Int Fputc ( Int C, file * Stream );  // Read the string with a length of-1 from the current position of stream and save it in S. S is returned. Otherwise, null is returned.  Char * Fgets ( Char * S, Int Size, file * Stream );  //  Write string to stream  Int Fputs ( Const   Char * S, file * Stream );  //  Read nmemb data, each of which is in size and saved in the space pointed to by PTR. Returned data size Size_t fread (Void * PTR, size_t size, size_t nmemb, file * Stream );  //  Extract data from the space pointed to by PTR and write nmemb data to stream. The size of each part is size, and the size of the written data is returned. Size_t fwrite ( Const   Void * PTR, size_t size, size_t nmemb, file * Stream );  //  Format read/write, the same as the printf and scanf of the standard input and output, but a file pointer is added before  Int Fprintf (File * stream, Const  Char * Format ,...);  Int Fscanf (File * stream, Const   Char * Format ,...);

 

Fopen returns a file * called a "file Pointer". A file is a system-defined structure that contains information such as the file name, File status, and current file status.Opening a file is actually about creating information about the file and directing the file pointer to the file for other operations. If the file is closed, the connection between the pointer and the file is closed, and operations on the file are prohibited.

 

The usage of related files is described as follows:

 

1. The file is comprised of R, W, A, T, B, and +. The meanings of each character are as follows:
R (read): Read
W (write): Write
A (append): append
T (text): Text File, can be omitted without writing
B (banary): Binary File
+: Read and Write

2. When you use "R" to open a file, the file must already exist and can only be read from the file.

3. A file opened with "W" can only be written to this file. If the opened file does not exist, the file is created with the specified file name. If the opened file already exists, delete the file and create a new file.

4. to append new information to an existing file, you can only open the file in "A" mode. However, this file must exist at this time; otherwise, an error will occur.

5. If an error occurs when opening a file, fopen returns a null pointer value. InProgramYou can use this information to determine whether the file is opened and process it accordingly.

 

6. when reading a text file into the memory, you need to convert the ASCII code into a binary code. When writing the file into a disk as text, you also need to convert the binary code into an ascii code, therefore, it takes a lot of time to read and write text files. This conversion does not exist for reading/writing binary files.

7. The standard input file (keyboard), standard output file (Display), and standard error output (error information) are opened by the system and can be used directly. Once the file close function fclose () is used, the application closes the file function to avoid errors such as file data loss.

Some instructions on file read/write:

1. During the read operation, the file must be opened in read or read/write mode. During the write operation, the file must be opened in write or append mode.

2. There is a position pointer inside the file to point to the current read/write position of the file. When the file is opened, the pointer always points to the first character position of the file.

3. For fgets, if an EOF or line break is encountered before the size-1 character is read, the reading ends.

4. fread and fwrite can be used to read the structure and write structure from the file.

 

Other file operations include:

 

C code
 //  Point the file location pointer to the position from whence plus offset. whence can be seek_set (start position of the file), seek_cur (current position), and seek_end (end position)  Int Fseek (File * stream, Long Offset, Int  Whence );  //  Returns the current location of the file.  Long Ftell (File * Stream );  //  Point the position pointer to the file header, which is equivalent to fseek (stream, 0l, seek_set) Void Rewind (File * Stream );  //  Get or set the file location pointer  Int Fgetpos (File * stream, fpos_t * Pos );  Int Fsetpos (File * stream, fpos_t * POS );

 

End

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.