我想读取存储在下面的文本文件中的数据
路径C:\folder1\folder2\example.txt
但我的代码不起作用。我收到消息“无法打开文件”但文本文件仍然存在。任何更正都将受到高度赞赏。
#include <windows.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string filename, line;
SetCurrentDirectoryA( "C:\\folder1\\folder2\\" );
ifstream inFile;
inFile.open("example.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> line) {
cout << line << endl ;
}
inFile.close();
return 0;
}
答案 0 :(得分:0)
#include <iostream>
#include <fstream>
int main()
{
char buf[256];
std::ifstream inFile("C:\\folder1\\folder2\\example.txt");
if (!inFile.is_open()) {
std::cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> buf) {
std::cout << buf << std::endl;
}
inFile.close();
return 0;
}
我刚刚尝试过它并且完美无缺