程序无法从变量打开文件
Program failing to open file from variable
我知道我在某处犯了一个愚蠢的错误,即使我一直在阅读旧问题,我也无法抓住它。我希望有人能为我指出正确的方向。正如你可能知道的那样,我是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 一起使用。
相关文章:
- 通过多个头文件使用常量变量
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 无法通过头文件保存变量的值
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在 C++ 中将双精度变量写入二进制文件
- 如何使用文件中的文本,例如变量
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 复制文件流C++静态变量
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- C++将文本从.txt文件解析为变量
- 从 C 中的变量获取文件的路径,C++
- 使用boost ::日志配置文件变量
- C++:帮助存储和显示输入文件变量
- 文件变量的问题
- 如何在先决条件列表中展开生成文件变量
- QT多个文件c++变量