In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you the detailed usage of C++ ofstream and ifstream. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
First, the two important operators of the stream class 1, the inserter (x;) means to read a specified type of data from the standard input stream.
In C++, the manipulation of files is achieved through the subclass fstream (file stream) of stream, so to manipulate files in this way, you must add the header file fstream.h.
Second, common file operations 1. Open files
In the fstream class, there is a member function open (), which is used to open a file, and its prototype is:
Void open (const char* filename,int mode,int access)
Parameters:
Filename: name of the file to open
Mode: how you want to open a file
Access: properties of the open file
The way to open a file is defined in the class ios, which is the base class for all streaming Imax O classes.
Common values are as follows:
Ios::app: open a file as an append
Ios::ate: navigate to the end of the file after the file is opened, and ios:app contains this attribute
Ios::binary: open the file in binary mode, which is text by default. For the difference between the two ways, see above.
Ios::in: file opening as input (file data input to memory)
Ios::out: file is opened as output (memory data is exported to file)
Ios::nocreate: no file is created, so it fails to open if the file does not exist.
Ios::noreplace: do not overwrite the file, so if the file exists failure when opening the file
Ios::trunc: if the file exists, set the file length to 0
You can connect the above attributes with "or", such as ios::out | ios::binary
The values for the properties of the open file are:
0: normal file, open and access
1: read-only fil
2: implied file
4: system Fil
You can connect the above attributes with "or" or "+", such as 3 or 1 | 2 to open the file with read-only and implicit attributes.
For example:
Open the file c:\ config.sys as binary input
Fstream file1
File1.open ("c:\\ config.sys", ios::binary | ios::in,0)
If the open function has only one argument to the file name, it opens as a read / write normal file, that is:
File1.open ("c:\\ config.sys"); file1.open ("c:\\ config.sys", ios::in | ios::out,0)
In addition, fstream has the same constructor as open (). For the above example, you can open the file when it is defined:
Fstream file1 ("c:\\ config.sys");
In particular, fstream has two subclasses:
Ifstream (input file stream) and ofstream (outpu file stream)
Ifstream opens the file as input by default
Ofstream opens the file as output by default.
Ifstream file2 ("c:\\ pdos.def"); / / Open the file ofstream file3 ("c:\\ x.123") as input; / / Open the file as output
Therefore, in the practical application, according to the different needs, choose different classes to define:
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 / output, use fstream to define it.
2. Close the file
Open files must be closed after use. Fstream provides a member function close () to complete this operation.
Such as: file1.close ()
Close the files connected to file1.
3. Read and write files
Read and write files are divided into text files and binary files.
It is easy to read a text file, just use an inserter and a parser
It is more complicated to read binary, so we will introduce these two methods in detail.
(1) Reading and writing of text files:
Use the inserter () to import from the file.
Suppose file1 is opened as input and file2 as output.
Examples are as follows:
File2i;// enters an integer value from the file.
This method also has a simple formatting ability, for example, you can specify the output to hexadecimal, and so on, the specific formats are as follows
Manipulator function input / output
Dec formatted as decimal numerical data input and output
Endl outputs a newline character and refreshes the stream output
Ends output an empty character output
Hex formatted as hexadecimal numeric data input and output
Oct formatted as octal numeric data input and output
Setpxecision (int p) sets the precision digit output of floating-point numbers
Example:
# include # include using namespace std;void main (void) {/ / use the constructor of the ofstream class to create a file output stream object to open the file ofstream fout ("d:\\ mytest.txt"); if (! Fout) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.