This article transferred from: http://blog.csdn.net/fanghb_1984/article/details/7425705
This article describes two methods to read and write files: 1, the use of FStream class, 2, the use of CStdioFile class.
CStdioFile inherits from CFile, a CStdioFile object represents a C run stream file that is opened with the run-time function fopen.
The FStream class in C + +, all I/O is based on this "stream" class, including the file I/O we want to know. The stream class has two important operators: the Insert (<<) and the accessor (>>). The Insert (<<) outputs data to the stream, and the profiler (>>) enters data from the stream. In C + +, the operation of the file is implemented through the subclass FStream (file stream) of the stream, so to manipulate the file in this way, the header file fstream.h must be added. The FStream class includes the Ofstream class that outputs data to the stream and the Ifstream class that outputs data from the stream.
1. Write the file
To write data to a file is relatively simple, here in the FStream class to file write data as an example, VC + + in the file writing method. For writing to files using the CStdioFile class, refer to the relevant data. The following is a code that uses the FStream class to write to a file:
#include <fstream.h>
Ofstream of1;//Creating objects
Of1.open ("Data record. txt", ios::out,filebuf::openprot);//Open File
int i;//defines an integer variable
float F; Define a single-precision floating-point variable
Double D; Define a double-precision floating-point variable
i=123;//Assignment Value
f=3.478f; Assign value
d=859.653; Assign value
of1<<i<< ' \ t ' <<f<< ' \ t ' <<d;//write data
Of1.close ();//Close file
After running, open the "data log. txt" file, as shown in Figure 1, the variable i,f,d is written to the file.
Figure 1 The contents of the Open file
2, the file read 2.1 uses the FStream class
Use the FStream in the Class (>>) to read the data from the file "data record. txt" generated in the first section, implementing the following code:
Ifstream IF1; Creating objects
If1.open ("Data record. txt", ios::in,filebuf::openprot);//Open File
int i; Define an Integer variable
float F; Define a single-precision floating-point variable
Double D; Define a double-precision floating-point variable
CString str; Define a CString class
while (!if1.eof ())
{
if1>>i>>f>>d;//reading data
Str. Format ("%d%f%lf", i,f,d);
MessageBox (str);
}
If1.close ();//Close file
The running results are shown in Figure 2.
Figure 2 Reading the results of a file
It is important to note that the Ifstream (>>) in the class is only applicable when the data is separated from the data by an empty (') or tab (' \ t '), for other delimiters, such as ', ', '; ' And so on, if you still read as described above, an error occurs. To solve this problem, this article introduces the second method of data reading.
2.2 Using the CStdioFile class
Using the CStdioFile class to read data from the file "data record. txt" generated in the first section, the following code is implemented:
CString string,str;//definition of two CString classes
int i; Define an Integer variable
float F; Define a single-precision floating-point variable
Double D; Define a double-precision floating-point variable
CStdioFile file ("Data record. txt", cfile::moderead);//Create CStdioFile Object
while (file. ReadString (String)//reads all characters from a line
{
String. Replace (', ', '); Replace the comma ', ' with a space ' in the character
String. Replace ('; ', ');//The semicolon in the character '; ' Replace with a space ' '
SSCANF (String, "%d%f%lf", &i,&f,&d);//Extract data
Str. Format ("%d%f%lf", i,f,d);
MessageBox (str);
}
File. Close ();//Closing file
The results of the program run are the same as in Figure 2.
Code snippet string. Replace (', ', ') and string. Replace ('; ', ') will read the comma in the string ', ' and semicolon '; ' Replace with a space ', which lays the groundwork for extracting data later. Therefore, using the CStdioFile class to read data from a file is more flexible than using the FStream class.
Go Write and read files in VC + +