c++将文件内容打印到屏幕
以下是一种实现方式:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string filename = "example.txt";
std::ifstream file(filename);
if (file.is_open())
{
std::string line;
while (std::getline(file, line))
{
std::cout << line << std::endl;
}
file.close();
}
else
{
std::cout << "Failed to open file: " << filename << std::endl;
}
return 0;
}
这个程序首先打开一个文件,然后使用 std::getline 函数逐行读取文件内容,并将每行内容打印到屏幕上。最后关闭文件。如果文件打开失败,则输出一条错误消息
原文地址: https://www.cveoy.top/t/topic/g07h 著作权归作者所有。请勿转载和采集!