程序无法从变量打开文件

Program failing to open file from variable

本文关键字:文件 变量 程序      更新时间:2023-10-16

我知道我在某处犯了一个愚蠢的错误,即使我一直在阅读旧问题,我也无法抓住它。我希望有人能为我指出正确的方向。正如你可能知道的那样,我是C++新手。

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
  //local variables
  string answer, filePath, wordtest;
  fstream openFile;
  ***stuff removed for space reasons***
  cout << "Enter the full file path" << endl;
  getline(cin, filePath);
  openFile.open(filePath, ios::in);         //Open file
  while (openFile.peek() != EOF)
  {
    cin >> wordtest;
    cout << wordtest;
    //getline(cin, wordtest);
    {
      //wordCount = wordCount + 1;
    }
  }
  openFile.close();
  openFile.clear(std::ios_base::goodbit);
  cout << "Loaded file and read " << wordCount << " words";
}

你既不是在读也不是在写openFile
您需要将operator<<operator>>与 openFile 一起使用。