在工程目录下用记事本新建了一个txt文件,try.txt,然后用vs读取该txt文件
string file = "try.txt";
ifstream in;
in.open(file.data());
if (!in.is_open())
{
cout << "can't open the txt"<<endl;
}
string line;
cout << "read the txt" << endl;
while(getline(in, line))
{
cout << line;
}
in.close();
程序感觉没有问题,但是一直失败,最后终于发现原因:
新建的txt文件,扩展名没有显示,显示出来后会是try.txt.txt,所以读取失败。所以一定注意新建的文件名的扩展名是否相符。
记事本新建的时候直接保存为try,默认为try.txt