Standard C ++ fstream

Source: Internet
Author: User

<Fstream> and <fstream. h>

<Fstream. h> supports IOs: nocreate and IOs: noreplace tags. <Fstream> the library does not support these tags. However, this article describes how to easily copy their functions.

File relocation

Each file object has a logical pointer pointing to a certain position offset in the file. You can call the seekp () member function to specify the logical pointer to any position in the file. The seekp () member function uses the specified position offset to locate the file again. In the following example, the program locates the File Location of 10th bytes, and then calls tellp () to output the new location:

Ofstream fout ("parts.txt ");
Fout. seekp (10); // move 10 bytes ahead from beginning
Cout <"New Position:" <fout. tellp (); // display 10

The seekp () member function has another value assignment method, that is, the file offset direction is used as the second variable. For example, this function can move two bytes backward from the current position to the new position:

Fout. seekp (-3, IOS: cur );

You can also use the following marker to specify the direction of the file location:

IOS: Beg // start position of the file
IOS: cur // current location, for example: IOS: cur + 5
IOS: End // position at the end of the file


Combine read and write operations
The <fstream> class can use the <and> operator to execute all data types, such as STD: string and STD: complex. Table C shows how to use these operators to perform a combined read and write operation. The program opens a file, writes two fields, returns to the initial position of the file, and then reads the original domain to the time_t and STD: string objects.

Supports multiple functions
<Fstream> another notable feature of the class library is the ability to support wchar_t. So far, each class I have discussed has a wchar_t equivalent that supports wchar_t data. However, standard C ++ only supports character-type file names, so these classes can only contain char * variables, regardless of their content. Table D displays the wchar_t version in table. As you can see, it is very easy to convert I/O to wchar_t Based on struct type.

<Stdio. h> and <fstream> are feasible options.
It must be admitted that <fstream> is not absolutely perfect, nor can it provide all the requirements of every programmer. For example, Unix developers who are used to file descriptors (rather than file names) may find <fstream> very cumbersome because there is no standard method to convert descriptors into file names. Similarly, if wchar_t file names are not supported, many obstacles may be encountered in certain environments. However, if you choose <fstream>, you will feel that this is a more intuitive, stable, and convenient method.

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.