File Operations in C/C ++ (2)

Source: Internet
Author: User

C ++ file operations.
Stream is introduced in C ++. The related header file <fstream> supports file input and output, and two <ifstream> and <ofstream> support File Reading and Writing respectively.

File opening and closing
Fstream is an object whose operations are completed by constructor and member functions.
Fstream (); explicit fstream (const char * filename, ios_base: openmode mode = ios_base: in | ios_base: out );
Fstream f ("input.txt", fstream: in | fstream: out );
The constructor can directly specify the name of the file to be opened, specify the open mode, input or output, or both input and output, or append or binary mode, you can use | for combination. When the open mode is not specified, the default value is used. in | out is available at the same time.
If you do not open the file during construction, you can use the open member function to open the file. The open parameter is similar to the constructor parameter. But it does not return values.
Void open (const char * filename, ios_base: openmode mode = ios_base: in | ios_base: out );
Fstream f;
F. open ("input.txt", fstream: in | fstream: out)
If you want to determine whether the file is successfully opened, you need to use is_open to determine whether the file is open.
The member function close is used to close the file.

Read and Write content
C ++ introduces Stream operations, stream operators> and <to read content from a file or write content into a file.
Istream & operator> (type & val); corresponds to fscanf. The formatted read content is determined by the value type.
When reading a string, it will read space or end with a line break.
There are also other format-controlled functions, such as hex reading in hexadecimal format, dec reading in decimal format, and skipws skipping white space.
The member function get can read a single character or a string, specify the read length, Terminator (generally '\ n'), or stream.
The member function getline reads a line of content to a string and specifies the string length (including the string Terminator '\ 0'). You can also specify the Terminator.
The member function ignore (streamsize n = 1, int delim = EOF) ignores n characters or ends with delim.
Read and readsome can also read the input.
Ostream & operator <(type val); stream output, always think that the C ++ output format is not as convenient as C's printf, it never takes a good look at how C ++ controls the output. Take a good look this time.
Hex/dec/oct output in 16/10/8 format
[No] showbase, (no) display the base prefix,
[No] showpoint, (no) indicates the decimal point,
[No] showpos, (no) display the positive number
Fixed, fixed Point Output
Scientific, scientific note
Left/internal/right, left/right aligned
Flush, output immediately
[No] unitbuf, (no) unit output,
[No] uppercase, all uppercase output,
Setw, set the width
You can also use the setf function to set the output format.
You can also use put, write, and other member functions for the output. <it should be enough.
Put, output a single character,
Write (char * buf, int size), write data blocks.

Let's take a look at the position of the pointer in the file.
Streampos tellp () to get the current location,
Seekp (streampos pos), set the current position to the file header, pos = 0,
Seekp (streamoffset offset, ios_base: seek_dir). Set the current position as the relative position. For details, refer to ios_base: beg/cur/end.

In C, ftell/fseek/fsetpos/rewind is also a function related to the position of the pointer in the file.

File input/output redirection

Unlike in C, files in C ++ are stream objects. They cannot be directly assigned to stdin or reopen functions, but are rdbuf () member functions used by cin or cout. If the file redirection ends and the cin needs to be restored to stdin, you can back up rdbuf of cin before the redirection. For details, refer to http://www.cnblogs.com/shilcare/archive/2010/09/02/1816062.html.

The sample code is as follows:

#include <iostream>#include <string>#include <fstream>using namespace std;int main(){    fstream fin("input.txt",fstream::in);    fstream fout("output.txt",fstream::out);
   streambuf *backup;      // backup the cin streambuf
  backup = cin.rdbuf(); cin.rdbuf(fin.rdbuf()); // input from fin cout.rdbuf(fout.rdbuf()); // output to fout char buf[256]; while(cin) { cin.getline(buf,256); // read a line from input.txt string line = buf; cout << line << endl; // output the line to output.txt }
  cin.rdbuf(backup); return 0;}

 

At this point, the file operation has basically ended.

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.