所以我正在开发一个类程序,我们必须打开两个不同的文本文件来检索要在控制台中显示的适当文本。我的代码没有打开文件并不断输出 else 语句“.txt 文件无法打开”。我尝试了几种不同的方法来打开文件,但没有运气。在这里的任何帮助将不胜感激。
//
// main.cpp
// PunchLine program
// Page 896 Problem 3
//
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//File stream objects
fstream inFile;
string line;
//Open joke file to read lines to console
inFile.open("joke.txt", ios::in);
if (inFile.is_open())
{
//Read lines from file to console
while (getline(inFile, line))
{
cout << line << endl;
inFile.close();
}
}
else
cout << "joke.txt file cannot be open.\n";
//Open punchline file to read last line joke to console
inFile.open("punchline.txt", ios::in);
if (inFile.is_open())
{
//Read last line from file to console
inFile.seekp(-52L, ios::end);
getline(inFile, line);
}
else
cout << "punchline.txt file cannot be open.\n";
return 0;
}