对文件的操作
C++读写文件通过fstream、ifstream、ofstream进行操作,文本文件用<< 和 >> 进行读写,二进制文件用read和write进行读写
1、从文件中读取数据到字符串
需要包含头文件加入#include<fstream> #include<sstream>
//从文件读入到string里
string readFileIntoString(char * filename)
{
ifstream ifile(filename);
//将文件读入到ostringstream对象buf中
ostringstream buf;
char ch;
while (buf&&ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
return buf.str();
}
2、将vector里面的数据存放到文件中,以每一行一个数据存放
void writeToFile(vector<string> operation)
{
std::ofstream outfile;
outfile.open("data.txt"); //创建、打开文件
for (vector<string>::iterator it = operation.begin(); it != operation.end(); it++){
outfile<< *it << endl;
}
outfile.close(); //关闭文件
}
3、文本文件写入
//采用CPP模式写txt
void TxtWrite_CPPmode()
{
//准备数据
int index[50] ;
double x_pos[50], y_pos[50];
for(int i = 0; i < 50; i ++ )
{
index[i] = i;
x_pos[i] = rand()%1000 * 0.01 ;
y_pos[i] = rand()%2000 * 0.01;
}
//写出txt
fstream f("txt_out.txt", ios::out);
if(f.bad())
{
cout << "打开文件出错" << endl;
return;
}
for(int i = 0; i < 50; i++)
f << setw(5) << index[i] << "\t" << setw(10) << x_pos[i] <<"\t" <<setw(10)<< y_pos[i] << endl;
f.close();
}
4、文本文件读取
//采用CPP模式读取txt
void TextRead_CPPmode()
{
fstream f;
f.open("txt_out.txt",ios::in);
//文件打开方式选项:
// ios::in = 0x01, //供读,文件不存在则创建(ifstream默认的打开方式)
// ios::out = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式)
// ios::ate = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用
// ios::app = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后
// ios::trunc = 0x10, //在读写前先将文件长度截断为0(默认)
// ios::nocreate = 0x20, //文件不存在时产生错误,常和in或app联合使用
// ios::noreplace = 0x40, //文件存在时产生错误,常和out联合使用
// ios::binary = 0x80 //二进制格式文件
vector<int> index;
vector<double> x_pos;
vector<double> y_pos;
if(!f)
{
cout << "打开文件出错" << endl;
return;
}
cout<<"mode为1,按字符读入并输出;mode为2,按行读入输出;mode为3,知道数据格式,按行读入并输出"<<endl;
int mode = 1;
cin>>mode;
if(1== mode)
{
//按字节读入并输出
char ch;
while(EOF != (ch= f.get()))
cout << ch;
}
else if(2 == mode)
{
//按行读取,并显示
char line[128];
int numBytes;
f.getline(line,128);
cout << line << "\t" << endl ;
f.getline(line,128);
cout << line << "\t" << endl ;
f.seekg(0,0); //跳过字节
//seekg(绝对位置); //绝对移动, //输入流操作
//seekg(相对位置,参照位置); //相对操作
//tellg(); //返回当前指针位置
while(!f.eof())
{
//使用eof()函数检测文件是否读结束
f.getline(line,128);
numBytes = f.gcount(); //使用gcount()获得实际读取的字节数
cout << line << "\t" << numBytes << "字节" << endl ;
}
}
else if(3 == mode)
{
//如果知道数据格式,可以直接用>>读入
int index_temp = 0;
double x_pos_temp = 0, y_pos_temp = 0;
while(!f.eof())
{
f >> index_temp >> x_pos_temp >> y_pos_temp ;
index.push_back(index_temp);
x_pos.push_back(x_pos_temp);
y_pos.push_back(y_pos_temp);
cout << index_temp << "\t" << x_pos_temp << "\t" << y_pos_temp << endl;
}
}
f.close();
}
5、QT中读取数据
// 读取数据
data.reserve(1262678);
char fileBuf[BUFSIZE] = { 0 };
QFile file(filePath);
if(!file.exists()) return;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
while (0 < file.readLine(fileBuf, BUFSIZE)) {
QString strLine = QString(QLatin1String(fileBuf));
QString s =strLine.simplified();
QStringList list = s.split(" ");
if(18 > list.size()) continue;
node temp;
qint8 col = 0;
temp.Vehicle_ID = list[col].toShort(); ++col;
temp.Frame_ID = list[col].toShort(); ++col;
temp.Total_Frames = list[col].toShort(); ++col;
temp.Global_Time = list[col].toLongLong(); ++col;
temp.Local_X = list[col].toDouble(); ++col;
temp.Local_Y = list[col].toDouble(); ++col;
temp.Global_X = list[col].toDouble(); ++col;
temp.Global_Y = list[col].toDouble(); ++col;
temp.v_Length = list[col].toDouble(); ++col;
temp.v_Width = list[col].toDouble(); ++col;
temp.v_Class = list[col].toShort(); ++col;
temp.v_Vel = list[col].toDouble(); ++col;
temp.v_Acc = list[col].toDouble(); ++col;
temp.Lane_ID = list[col].toShort(); ++col;
temp.Preceding = list[col].toShort(); ++col;
temp.Following = list[col].toShort(); ++col;
temp.Space_Headway = list[col].toDouble(); ++col;
temp.Time_Headway = list[col].toDouble(); ++col;
data.push_back(temp);
memset(fileBuf, 0x00, BUFSIZE);
}
file.close();
//test
// node temp = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
// data.push_back(temp);
// temp = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
// data.push_back(temp);
}