程序无法从变量打开文件

时间:2016-01-29 20:47:32

标签: c++

我知道我在某个地方犯了一个愚蠢的错误,即使我一直在读老问题我也无法理解。我希望有人能指出我正确的方向。你可能会说我是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";

}

1 个答案:

答案 0 :(得分:2)

您既不会阅读也不会写信openFile 您需要在openFile中使用operator<<operator>>