C-language file operations

Source: Internet
Author: User
Tags processing text rewind

C-language file operations

1. Data Stream:
Interaction between programs and data is performed in the form of a stream. Fopen indicates opening the data stream, and fclose indicates refreshing the data stream.
The so-called data stream is an abstraction that indicates that the data segment needs to be received gradually like a stream and cannot be accessed randomly. It also means that it is a continuous piece of content, and the order between each piece of data is determined. For example, a large file cannot be loaded to the memory at a time, and the content of the file can only be gradually loaded to the memory.
For example, TCP is abstracted as stream and UDP is abstracted as block.

2. buffer zone:
When fopen is enabled, the disk --> buffer;
When fread is used, the buffer --> program;
During fwrite, the program --> buffer;
Fclose, buffer --> disk;
When reading a file, it does not directly read the disk. Instead, it first opens the data stream, copies the file information on the disk to the buffer zone, and then the program reads the data from the buffer zone;
When writing a file, the disk is not directly written, but the buffer is written first. Data is written to the disk only when "the buffer is full" or "close the file;

It seems that no buffer can be set. In this case, low-level I/O functions are used to directly operate the disk, which is slow. It is not a standard function and is not suitable for cross-platform operations.


3. Text and binary modes:
When fopen is enabled, you can specify the file opening mode. "T" indicates the text mode, and "B" indicates the binary mode;
To open a file in text mode, convert \ r \ n (line feed) in the file to \ n. When saving the file to the disk, \ n is converted to \ r \ n;

Why does it distinguish between the text mode and the binary mode?
Before the computer appears, use a typewriter to input text. After writing a line, you need to perform two operations: return and next. Press enter to move the print header to the beginning of the line, line feed refers to moving the print header to the next line. Later, the computer invented it. Some people thought that the line feed would take two characters, which was a waste of time. Just add one character, which leads to a divergence.
In Windows, when operating a text file in the form of a typewriter, if you need to wrap the line, you must enter two characters \ r, \ n at the end of the string;
Linux doesn't think so. When you need to wrap a line, add a character \ n to the end of the string;
Later, ANSI stipulated that \ n in C is a "logical newline operator" in text mode. During read/write operations, it can be converted between the platform and physical storage.
According to ANSI, windows must be compatible with the previous text files. When implementing the C Runtime Library, when the text is opened and saved, the original \ r \ n is converted;
The text mode in Linux is no different from the binary mode.
Mac OS text mode \ r represents a new line ......

Sometimes in vs2008, "Do You Need To standardize the end of a line ?" This prompt allows you to select windows (CRLF) because the code at the end of the line of the file is not \ r \ n. In this case, select OK or cancel, only the code at the end of the line is unified as \ r \ n, and no problems will occur.

When to operate files in text mode?
I think the text mode is only required when processing text documents. It is good to parse configuration files and generate log files in binary mode.

Note:
The text file should only be opened in text mode; the binary file should only be opened in binary mode; otherwise, unexpected errors may occur;
If a text file needs to be used repeatedly between Windows and Linux systems, the line break should be converted before the file is operated;

4. File pointer:
Most of the reading and writing functions move the position of the file pointer.
Main functions:
Fseek (); // set the file pointer position
Ftell (); // get the current location
Rewind (); // adjust to the beginning
MACRO:
Seek_set // start
Seek_cur // current location
Seek_end // end

Note:
To maintain compatibility with read/write operations that were not allowed at the same time in the past, an operation cannot be followed directly by an output operation, and vice versa. To perform both input and output operations, you must insert a call to the fseek function.
When you use R +, W +, A +, and other read/write methods to open a file, there must be an fseek between the read and write operations to convert the read/write status. Otherwise, there may be uncertain situations:
Fgets (BUF, 6, pfile );
Fseek (pfile, 0, 0); // call fseek immediately to adjust the file pointer to the appropriate location
Fputs ("sosad", pfile );

5. EOF (end of file)
EOF is a macro. Many functions return this macro to indicate that the file has reached the end or an error occurs during function execution. For example, fputs
Feof (File *) is a function used to determine whether the object has reached the end;

6. C language file operation function summary:
Close: fopen, fclose, and freopen (you can specify the file mode again)
Read/write: fgetc, fputc, fgets, fputs, fread, fwrite, fprintf, fscanf, GETC, putc, getchar, putchar, gets, puts...
Buffer operations: fflush, setbuf, setbuffer, setlinebuf, setvbuf...

7. Examples of common functions:

void ReadBinaryFileToStr(const char* path, std::string& outStr){    FILE* pFile = fopen(path, "rb");    fseek(pFile, 0, SEEK_END);    long lsize = ftell(pFile);    rewind(pFile);        char* pBuf = new char[lsize+1];    int ret = fread(pBuf, 1, lsize, pFile);    if (ret == lsize)    {        pBuf[lsize] = ‘\0‘;        outStr.append(pBuf);        delete[] pBuf;    }    fclose(pFile);}void WriteBinaryFileFromStr(const char* path, std::string& inStr){    FILE* pFile = fopen(path, "wb");    fwrite(inStr.c_str(), sizeof(char), inStr.length(), pFile);    fclose(pFile);}

 

References:

Http://blog.csdn.net/xiaodan007/article/details/8350193

Http://blog.csdn.net/silyvin/article/details/7275037

Http://xu020408.blog.163.com/blog/static/26548920094673814288/

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.