Talk C Chestnut Bar (144th: C-language Example-file operation Finale)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about file manipulation based on file pointers, this time we're talking about a file operation finale. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we're in front of you. Zhanghuizhong describes two ways to file operations: file descriptors and file pointers . Today we will summarize and contrast these two file operations.

Let's first look at what they have in common:

    • 1. tastes the same : all like to eat files (can be used to manipulate files);
    • 2. The same way of life : Sunrise, Sunset (using them is performed according to the file operation three steps: Open, operate, close);
    • 3. No worry : easy to cause errors when using, need to check the results after use, in order to avoid errors;

Let's take a look at their different points:

    • 1. different parents : The file descriptor is the Linux system "raw", and the file pointer is C standard library "health";
    • 2. height is different : file descriptor is small, walk little steps, walk the same way is always more than the others walk the number of steps. The file pointer is tall, the step is also big, walks the same road when walks the number of steps few. You don't believe me? Let me tell you why: When manipulating a file with a file descriptor, operations are performed in bytes, so the read/write is more frequently read/write than the same data. When using a file pointer, the unit of the operation can be defined by itself, and if the operating unit is larger than the byte, then read/write the same data, the number of reads/writes is less.
    • 3. different personality : file descriptor is a chronic child, the efficiency of the operation of the file is low, and the file pointer is a short-tempered, work Fengfenghuohuo, the efficiency of the operation of the document is high. They have such personalities, and they have a great relationship with their parents. As we all know, both the computer's memory and the hard disk can hold files, but it is faster to read the files from memory than to read them from the hard disk, because the memory reads much faster than the hard disk. File descriptor It is the Linux system, let the file descriptor directly to the disk file operation (in fact there is a small buffer, but can be ignored), and the file pointer it parents (C standard library) Let the file pointer in memory buffer operation file, When the buffer is manufactoring, the data in the buffer is transferred to the file on the hard disk, which reduces the number of times the file pointer reads the hard disk, thus improving the efficiency of the file operation.

Crossing, there are two ways to file operations: file descriptors and file pointers, and that's all we're talking about. In the actual work, you can combine their similarities and different points to choose one to manipulate the file. However, based on experience, it is better to manipulate files using the file pointers provided by the standard library, which will not only improve the efficiency of file operations, but also improve the portability of the programs. Of course, using file descriptors to manipulate files is the best choice if you want to manipulate and control your files.

Crossing, here's an example of a file operation finale. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (144th: C-language Example-file operation Finale)

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.