c++ 读写txt
当创建ofstream对象后,可以像操作cout一样操作这个对象,也就是可以把ofstream的对象当做cout一样进行输出。
ofstream OutFile("Test.txt");
OutFile << "This is a Test12!";
OutFile.close();
当创建ifstream对象后,可以像操作cin一样操作这个对象,也就是可以把ifstream的对象当做cin一样进行输入。
ifstream fAssociation;
fAssociation.open(strAssociationFilename.c_str());
while(!fAssociation.eof()){
string s;
getline(fAssociation,s); //读取每一行的数据
if(!s.empty()){
stringstream ss;
ss << s; //每一行有三个句子,以空格隔开
double t;
string sRGB, sD, sRect;
ss >> t;
vTimestamps.push_back(t);
ss >> sRGB;
vstrImageFilenamesRGB.push_back(sRGB);
ss >> sD;
vstrImageFilenamesD.push_back(sD);
ss >> sRect;
vstrImageFilenamesRect.push_back(sRect);
}
}