【发布时间】:2013-12-16 19:33:32
【问题描述】:
我试过四处打听并在谷歌上搜索答案。它不会在我的文档文件夹中创建 .txt 文件。无论我尝试什么,我都没有收到任何错误,并且代码一直运行到 main 结束。 1. 我的输入是正确的。我检查了cout。 2. 我把 C:\users\Bryan\Documents\Points.txt 作为我的目录
int main()
{
//...
std::string filename;
cout << "Enter output filename: ";
std::getline(cin, filename);
ofstream ost(filename.c_str());
if (!ost) cerr << "can't open output file: " << filename << endl;
for(int i=0; i<points.size(); ++i)
ost<<'('<<points[i].x<<','<<points[i].y<<')'<<endl;
cout <<"got here 6"<<endl;
//...
}
我添加了 close(),但忘记了返回 0,并且它工作了一次。 然后我添加了return 0,无论我尝试多少次,它都不会创建新文件,但不会抛出错误。 我看不出我做错了什么。有人吗?
int main()
{
cout <<"got here 1"<<endl;
cout << "Please enter the file name: ";
char name[90];
cin.getline(name, 90);
cout <<"got here 2"<<endl;
ifstream ifs(name);
if(!ifs) error("can't open input file ",name);
vector<Point> points;
Point p;
while(ifs>>p)points.push_back(p);
cout <<"got here 3"<<endl;
for(int i=0; i<points.size(); ++i)
cout<<'('<<points[i].x<<','<<points[i].y<<')'<<endl;
std::string filename;
cout << "Enter output filename: ";
std::getline(cin, filename);
ofstream ost(filename.c_str());
if (!ost) cerr << "can't open output file: " << filename << endl;
for(int i=0; i<points.size(); ++i)
ost<<'('<<points[i].x<<','<<points[i].y<<')'<<endl;
cout <<"got here 6"<<endl;
ost.close();
keep_window_open();
return 0;
}
【问题讨论】:
-
可能是文件出了问题(损坏或类似情况)。您是否尝试重新启动计算机?
-
我很确定这是愚蠢的 Avast 防病毒软件。它总是要求我批准运行应用程序,并阻止许多程序,例如,当我连接智能手机时,防火墙会阻止我的互联网连接。
-
仅供参考:我又试了一次。 Avast 中断 MS c++ 编译器让我可以选择在沙盒中运行,或者正常运行。一开始我选择了沙盒,因为它只是少了一次鼠标点击。我没有理由不这样做,因为我一直在关注 Stroustrup 的教科书,而且本书第一部分的简单程序不受沙盒的影响。我猜 Avast 的沙盒会阻止创建任何新文件。