This article mainly introduces: C + + use Ifstream and ofstream function to read and write TXT file, including matrix and string reading and writing.
Description
The header files that need to be added are: #include <sstream>
#include <iostream>
#include <fstream>
#include <string>
You also need to add namespaces: using namespace std;
First, read the matrix
Sometimes the TXT store is a matrix element, and if the matrix element is spaced between spaces, it can be read with the following program:
Ifstream infile;//define file variable
infile.open (txtpath,ios::in);//Open Txtpath as file path
if (!infile)
{
AfxMessageBox ("Failed to read TXT file.") ");
return FALSE;
}
String temp,temp2mat;//defines an intermediate variable
while (Getline (infile,temp))//reads a row, while loop, until the last line of the file
{
Istringstream Lineband (temp);//convert to data stream
while (Lineband>>temp2mat)///sequentially output a number to Temp2mat until the last number of the line
{
int Value = Atoi (Temp2mat.data ());//convert Temp2mat from character to Integer, if floating-point number, atoi to Atof
}
}
infile.close ();//Close file
Second, output matrix
Ofstream outfile;//define file variable
outfile.open (outpath,ios::out);//create file, outpath Create txt path
if (!outfile)
{
AfxMessageBox ("Create TXT file failed.) ");
return FALSE;
}
for (i=0;i<linenum;i++)/write each line sequentially, linenum the number of rows to write
{
for (j=0;j<rownum;j++)/write numbers in sequence, separated by spaces, RowNum the number of digits in the row
outfile<<x<< ""; Write a number of space intervals x
outfile<< "\ n";//write line newline
}
Outfile.close ()//close file
Note : Sometimes it is possible to write the last time after writing, and then only need to modify the type of open file: Outfile.open (outpath,ios::out | ios::app); Ios::app append at end of file
Related other types can be referred to blog: http://www.cnblogs.com/zhcncn/archive/2013/01/08/2851656.html
Third, read the string
You can read a string or a Chinese character.
Ifstream infile; Definition file
infile.open (inpath,ios::in);//inpath txt path
if (!infile)
{
AfxMessageBox ("read feature file failed.") ");
return;
}
string temp;
Char str[64];
CString str2;
while (Getline (infile,temp))//reads a row sequentially until the file ends
{
memset (str, ' + ', sizeof (str));//read result to char*
type memcpy (Str,temp.c_str (), temp.size ());
STR2 = Temp.c_str (); The read result is assigned to the CString type
}
infile.close ();//Close file
Iv. Writing strings
Ofstream outfile;//define file variable
outfile.open (inpath,ios::out);//create file, outpath Create txt path
if (!outfile)
{
AfxMessageBox ("Create TXT file failed.) ");
return;
}
int linenum = 1; Write number of rows
char str[] = "txt";
CString str2 = "Hello";
for (int i=0;i<linenum;i++)/write each line sequentially, linenum the number of rows to write
{
outfile<<str;
outfile<< "\ n"; Write str string
outfile<<str2;
outfile<< "\ n"; Write CString string
}
Outfile.close ()//close file