Read and write operations on the file is what we often use in the project, in the online read a lot of blogs, combined with their own project experience summed up a bit, so wrote this blog, some places may be directly copied from other blogs, but will indicate the source.
I. Input and output of the file
FStream provides three classes for implementing C + + operations on files. (file creation, reading, and writing).
Ifstream--read from an existing file
Ofstream-Write content to a file
FStream-Open file for read and write
File Open mode:
Ios::in Read Only
Ios::out Write only
Ios::app write from the end of the file to prevent the loss of the original content in the file
Ios::binary binary mode
Ios::nocreate you open a file, if the file does not exist, the file is not created
Ios::noreplace when you open a file, if the file does not exist, create the file
Ios::trunc open a file, and then empty the content
Ios::ate when you open a file, move the location to the end of the file
The use of the file pointer position in C + +:
Ios::beg file Header
Ios::end file Footer
Ios::cur Current Location
Example:
FILE.SEEKG (0,ios::beg); To position the file pointer at the beginning of the file
FILE.SEEKG (0,ios::end); To position the file pointer to the end of the file
FILE.SEEKG (10,ios::cur); Let the file pointer move 10 bytes from the current position to the end of the file
FILE.SEEKG ( -10,ios::cur); Let the file pointer move 10 bytes from the current position to the beginning of the file
FILE.SEEKG (10,ios::beg); Position the file pointer to a location 10 bytes from the beginning of the file
Note: The units that are moved are bytes, not rows .
Common methods of error determination:
Good () If the file opens successfully
Bad () An error occurred while opening the file
EOF () reaches end of file
The above information is from: http://www.cnblogs.com/helinsen/archive/2012/07/26/2609251.html
Here is an example of reading a string from a hello.txt file and writing to the OUT.txt:
#include <vector> #include <string> #include <fstream> #include <iostream> using namespace std; int main () { ifstream myfile ("g:\\c++ project\\read\\hello.txt"); Ofstream outfile ("g:\\c++ project\\read\\out.txt", Ios::app); string temp; if (!myfile.is_open ()) { cout << "File not successfully opened" << Endl; } while (Getline (myfile,temp)) { outfile << temp; outfile << Endl; } Myfile.close (); Outfile.close (); return 0; }
The contents of the Hello.txt file are:
After the program executes 3 times, the contents of the OUT.txt file are:
The above code reads a string type of data, then what to do with the int type data, in fact, the truth is similar, the following examples illustrate:
#include <iostream> #include <string> #include <vector># Include <fstream>//file stream library functions using namespace Std;int cost[10][10];int main () {int V, W, Weight;ifstream infile; Input stream ofstream outfile; Output stream Infile.open ("g:\\c++ project\\read\\data.txt", ios::in); if (!infile.is_open ()) cout << "Open file Failure" << Endl;while (!infile.eof ())//If not to end of file loop {inf Ile >> v >> w >> weight;cost[v][w] = weight;cost[w][v] = weight; }infile.close (); Close file Outfile.open ("G:\\c++ project\\read\\result.txt", Ios::app); Each write is positioned at the end of the file, will not lose the original content, with out the original content will be lost if (!outfile.is_open ()) cout << "Open file Failure" << endl;for (int i = 0; I! = 10; ++i) {for (int j = 0; J! = ++j) {outfile << i << "\ t" << j << "\ T" << Cost[i][j] << Endl Write results in Result.txt}}outfile.close (); return 0;while (1);}
The function of the above code is to read the data of the Data.txt file, note that at this time the data in the Data.txt file is required to be three rows, each data is separated by a space, because in many projects, such as a certain algorithm game, according to the graph of the data to build the adjacency matrix or adjacency table, the data are arranged, in In the above code, V and W represent the vertex designator, weight represents the weight of the edge <v,w>, and the function of the above code is to construct the neighboring matrix of the graph described by the Data.txt file.
The data for the Data.txt file is as follows:
After the program runs, the contents of the Result.txt file are as follows:
Note: The data is too long and only part of it is given.
In fact Requirements data.txt files in the data are a bit high, if there are rows in the Data.txt file has two data, some rows have three data, some rows have 4 data, the above method will not work, in fact, change the above code on it, it is important that you understand when to read the row there are several data, the following Example Description:
Suppose the data in the data.txt file is as follows:
The data in the first row indicates that there are 5 rows of three data in each row, and in front, there are two rows of two data in each row, after which, except for the first line, the following is the true data, so the code to read the data is as follows:
#include <iostream> #include <string> #include <vector> #include <fstream>//File flow library function using Namespace Std;int cost[10][10];int Main () {int Num_3,num_2;int V, W, Weight;ifstream infile; Input stream ofstream outfile; Output stream Infile.open ("g:\\c++ project\\read\\data.txt", ios::in); if (!infile.is_open ()) cout << "Open file Failure" << endl;infile >> num_3 >>num_2; Read first line while (Num_3! = 0)//Read 3 data {infile >> v >> w >> weight;cost[v][w] = weight;cost[w][ V] = weight; num_3--;} while (num_2! = 0)//Read 3 data of {infile >> v >> w;cost[v][w] = 100;cost[w][v] = 100; num_2--;} Infile.close (); Close file Outfile.open ("G:\\c++ project\\read\\result.txt", ios::out); Each write is positioned at the end of the file, will not lose the original content, with out the original content will be lost if (!outfile.is_open ()) cout << "Open file Failure" << endl;for (int i = 0; I! = 10; ++i) {for (int j = 0; J! = ++j) {outfile << i << "\ t" << j << "\ T" << Cost[i][j] <<Endl Write results in Result.txt}}outfile.close (); return 0;while (1);}
For the convenience of inspection, we set the weight of those edges (i.e., those with only two data per row in the corresponding data.txt file) to 100, as shown in the code above:
The results show that the read is correct.
Note: The above code is run because the empty lines between each row do not seem to affect, such as the above Data.txt, the first row and the second row between the empty 1 rows or 2 rows are not related.
Two, astring stream
The following information is referenced from: http://blog.csdn.net/xujian_2014/article/details/42679525
The string header file defines three types to support memory Io,istringstream reading data to string, Ostringstream writing data from string, StringStream can read data from a string or write data to a string, just as a string is an IO stream.
1,the use of Istringstream, examples are as follows:
#include <string> #include <sstream> //header files required to use Istringstream #include <iostream> using namespace std; int main () { string str = "Hello word! I am Lee "; Istringstream is (str); Binds is to str string s; While (is >> s) { cout << s << endl; } return 0; }
The result of the above code operation is as follows:
This is equivalent to splitting a sentence into words, referring to the method of reading the string from the file mentioned earlier, if the string object being read is a sentence and contains many words, then we can use this method to split the string object.
2, the use of Ostringstream
Ostringstream is also constructed from a string object, and the Ostringstream class inserts a character into a string.
#include <string> #include <sstream> //header files required to use Ostringstream #include <iostream> using namespace std; int main () { ostringstream ostr; OSTR.STR ("abc");//If a string parameter is set at the time of construction, the increment operation does not increase from the end, but modifies the original data, and the excess portion grows ostr.put (' d '); Ostr.put (' e '); ostr<< "FG"; String gstr = Ostr.str (); Cout<<gstr << Endl; return 0;}
The results of the operation are as follows:
In the previous example code, we can continuously insert a single character or string into the ostr by using the put () or left-shift operator, and return the full string data after the growth through the STR () function, but it is worth noting that when the object is constructed with string data in it, Then the growth operation will not increase from the end, but modify the original data, beyond the growth of the part.
Late at night,,,
The weather in Guangzhou is always so fast, and it rains,,,
The use of string or int data and string streams in C + + read-write txt files