C++ 从文本文件读取数据并在控制台中显示
我想从存储在以下文件中的文本文件中读取数据C++ 从文本文件读取数据并在控制台中显示,c++,fstream,C++,Fstream,我想从存储在以下文件中的文本文件中读取数据 路径C:\folder1\folder2\example.txt 但是我的代码不起作用。我收到消息“无法打开文件”,但文本文件仍然存在。如有任何更正,将不胜感激 #include <windows.h> #include <fstream> #include <string> #include <iostream> using namespace std; int main() { string f
路径
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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文件名,行;
SetCurrentDirectoryA(“C:\\folder1\\folder2\\”;
河流充填;
open(“example.txt”);
如果(!infle){
cout>行){
不能包含
#包括
int main()
{
char-buf[256];
std::ifstream infle(“C:\\folder1\\folder2\\example.txt”);
如果(!infle.is_open()){
标准::cout>buf){
std::cout为什么不直接使用infle.open(“C:\\folder1\\folder2\\example.txt”);
?@NathanOliver我试过了,但没用
#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;
}