如何在C 中显示文件
How to display a file in c++?
本文关键字:显示文件 更新时间:2023-10-16
我需要有人编辑该代码,以便我可以显示一个文件!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string data; //enter a data as a string
ifstream datafile; // input datafile as ifstream
datafile.open("test.txt"); // open test file
}
不幸的是,您没有指定要读取和显示文件的方式。因此,我假设输出应该转到std::cout
。在附件的提案中,有两种可能读取的可能性:逐行行,因为您将在任何文本编辑器中读取文件,或在新行中分别读取每个单词(以白色空格分隔的输入,即空白,制表符或线路断裂(。
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string data; //enter a data as a string
std::ifstream datafile; // input datafile as ifstream
datafile.open("Source.cpp"); // open test file
// read line by line
while (std::getline(datafile, data))
{
std::cout << data << std::endl;
}
/*
// read each word (separated by white spaces)
while (!datafile.eof())
{
datafile >> data;
std::cout << data << std::endl;
}
*/
datafile.close();
return 0;
}
没有错误处理。如果文件不存在或其他任何发生的情况都不会抓住任何例外。
相关文章:
- gtkmm 显示文件内容
- 如何在 MFC 中显示文件的所有行
- 程序应该显示文件的最后5行,但它不适用于大文件
- 显示文件中具有最小给定元音数的单词
- 如何在 QT 中显示文件加载状态
- 如何在窗口中显示文件夹中所有文件的名称?Qt c++.
- 如何仅显示文件中的一个帐户?
- 如何在C 中显示文件
- 无法显示文件(与类有关)
- 如何禁用调试控制台在我的代码中显示文件地址
- 文件I/O-显示文件的内容
- qt5设计器,使用fileopen,在lineedit中显示文件路径,这样做是否有问题
- 如何在C++中显示文件的内容
- 在可视C++ 6 中显示文件夹选择对话框
- 如何在 c++ 中使用 system() 显示文件的内容
- 为什么如果数据已经发送,则选择仅显示文件描述符已就绪
- QFilesystemModel仅显示文件
- 读取和显示文件
- 在opencv中显示文件中的视频
- 读取并显示文件,但输出不正确