Intro CS类 - ifstream / ofstream奋斗C ++ Visual Studio

时间:2017-09-24 19:38:01

标签: c++ visual-studio-2017

上周我刚刚学会了使用文件进行编码,这让我很困惑。目标是让用户输入保存到文件的3个数字,然后在.txt文件打开并保存时写入3个附加数字。该程序运行但我遇到了4个问题。在我的"如果"声明,它说" outfile是未定义的"," outfile是未声明的","左边的' .fail'必须有class / struct / union"。此外,即使Visual Studio没有考虑到错误,我的最后一个cout语句也不会显示。我的代码看起来就像我教授的示例程序,我无法弄清楚。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile;
outFile.open("data.txt", ios::app);
int num1 = 0;
int num2 = 0;
int num3 = 0;
if (outfile.fail());
{
    cout << "File did not open.\n";
        return 1;
}
cout << "Enter three integers: \n";
cin >> num1;
cin >> num2;
cin >> num3;

outFile << num1 << endl << num2 << endl << num3;
outFile.close();
cout << "The data has been written to file.";
system("pause");
return 0;

}

0 个答案:

没有答案