C ++ common file read/write operations

Source: Internet
Author: User

1. There are three file IO stream classes: fstream, ifstream, and ofstream. the header file must be included: # include <fstream>;


2. in actual application, select different Io stream classes based on different needs. If you want to open it as input, use ifstream to define it; if you want to open it as output, use ofstream to define it; if you want to open it as input and output, fstream is used for definition;


3. The first way to open a file is through the constructor. That is, to open a file when defining a stream object, this mode requires passing in the file name and open mode when defining the object. Let's take a look at the constructor definitions of the three file IO stream classes:
Fstream (const char * filename, ios_base: openmode mode = ios_base: In | ios_base: Out );
Ifstream (const char * filename, ios_base: openmode mode = ios_base: In );
Ofstream (const char * filename, ios_base: openmode mode = ios_base: Out );
Note that the second parameter has a default value. fstream uses the input/output mode by default, ifstream uses the input mode by default, and ofstream uses the output mode by default. Therefore, if you do not have special requirements, you can directly define an IO Stream object to input the file name to be opened to perform Io operations.


4. The second way to open a file is through the member function open. All three file IO stream classes have a member function open, which is used to open files. Consider the prototype of the three open functions:
Void fstream: open (const char * filename, ios_base: openmode mode = ios_base: In | ios_base: Out );
Void ifstream: open (const char * filename, ios_base: openmode mode = ios_base: In );
Void ofstream: open (const char * filename, ios_base: openmode mode = ios_base: Out );
Note that the parameter list of the three functions is the same as that of the constructor of the corresponding class, so they are also used. In this way, first define an IO Stream object without passing in any real parameters, and then call the OPEN function. For example, to open a file for read operations, the Code is as follows:
Ifstream IFS;
IFS. Open ("FILENAME ");
......


5. The second parameter "open mode" of the above function is defined in the class IOs (which is the base class of all stream I/O classes). The common values are as follows:
IOS: APP: open a file in append Mode
IOS: ate: After the file is opened, it is located at the end of the file. IOS: app contains this attribute.
IOS: Binary: open a file in binary mode. The default mode is text. For the differences between the two methods, see the previous article.
IOS: In: open the file as input (input file data to memory)
IOS: Out: The file is opened as an output (memory data is output to the file)
IOS: nocreate: The file is not created, so opening fails if the file does not exist.
IOS: noreplace: Does not overwrite the file. Therefore, if the file fails to be opened
IOS: trunc: if the file exists, set the file length to 0.
You can use "or" to connect the preceding attributes, for example, IOS: Out | IOs: binary.


6. Check whether the file is successfully opened or whether the file stream is available.
First, the good member function is provided in the three classes to determine whether the stream is normal (no exception). In addition, you can directly determine whether the stream object is true or false. For example:
Fstream FS (...);
If (FS. Good ())
......
Ifstream ifs (...);
If (IFS) or if (IFS. Good (), you can use if (! IFS)
......


7. close the file. It's very simple. There's nothing to say, just call the member function close, for example, ifs. Close ().


8. file read/write. There are many ways to read and write files. Commonly used tools include the inserter (<) and analytics (>), get, put, read, and write. This is not detailed here, supplement the configuration when necessary.


9. The Getline function, which is often used for text analysis, is described here.
First, let's take a look at the prototype of the function:
Istream & Getline (istream & is, string & STR, char delim );
Istream & Getline (istream & is, string & Str );
Parameter: is the input stream for the read operation. Str stores the read content and delim is the final character.
Note: This function stores the characters read in the input stream is into STR and ends with the terminator delim. The first function delim is a Terminator that can be defined by the user. The second function delim is '\ n' by default (line break ). After the terminator delim is encountered, delim is discarded and not stored in Str. The next read operation starts to read the next character of the delim. When a function encounters an object terminator (EOF) in the input stream is or an error occurs while reading the object.
For example, to read the content of a text file by row, you can write it like this:

Ifstream ifs ("FILENAME ");
String strline;
While (Getline (IFS, strline ))
{
......
}
[Note] after each read to delim, the data is stored in STR, and the delim at the end is discarded, so STR does not have delim at last. Therefore, if you read a text file by row and then output it to the screen, you need to output an Endl if no line is output, as shown below:

Ifstream ifs ("FILENAME ");
String strline;
While (Getline (IFS, strline ))
{
Cout <strline <Endl; // line feed is required
}


10. There are also file pointer positioning, which is not detailed here and can be supplemented as needed.




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.