65,183
社区成员




本人正在用Compiler Explorer 学习C++,现遇到一个问题是:用程序打开本地计算机的文件时,无法读出data.txt中的内容,编译后没有任何输出。请教各有识之士,data.txt是否需要上传到Compiler Explorer 哪里保存,或者如果要读写本地的文件,in中的文件路径该怎么写?程序如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream in("d:\\data.txt"); //in中的文件路径该怎么写?
if(!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
string line;
vector<string> words;
while(getline(in,line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while(it != words.end())
{
cout << *it << endl;
++it;
}
return 0;
}
可能是文件编码不对,用记事本打开d:\data.txt,另存为,设置ANSI编码