从TextFile读取数据,并在控制台中显示
read data from textfile and display in console
我想从以下文本文件中读取数据
路径C:folder1folder2example.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;
}
#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;
}
我刚刚尝试过它,它可以完美
相关文章:
- 控制台输出在 Qt5 中未正确显示
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 为什么调试时没有显示控制台窗口
- 将控制台输出写入.txt并在控制台上显示
- 当被一个输入分开时,有两个控制台输出同时显示
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 在任何 Windows 版本上全屏显示控制台
- C++:显示控制台窗口的输出
- C++:如何在控制台窗口中显示结果
- 控制台输出显示ASCII值,而不是数字的预期数字
- C++/CLI 项目如何在控制台窗口中显示消息
- Linux C LIBEV官方示例显示了冗余控制台行为
- QProcess::startDetached() 不显示控制台窗口
- 如何在IDE中而不是在命令提示符中显示Visual Studio控制台应用程序输出
- 如何使用我的 if-else 语句根据用户权重的输入执行特定操作,然后将其显示到控制台?
- 在后台控制台窗口中显示光标
- 向控制台显示随机数
- 用于跨平台的C++控制台显示
- 使用_CrtDumpMemoryLeaks向控制台显示数据
- 如何在打开外部应用程序时阻止控制台显示