写一个函数将这个路径Dannouncementtxt的文件内容打印到屏幕c++
下面是一个简单的函数实现:
#include <iostream>
#include <fstream>
void printFileContent(const std::string& filePath) {
std::ifstream file(filePath);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cerr << "Unable to open file " << filePath << std::endl;
}
}
int main() {
std::string filePath = "D:\\announcement.txt";
printFileContent(filePath);
return 0;
}
这个函数使用了标准库中的 ifstream 类来打开指定路径的文件,并逐行读取文件内容输出到屏幕上。如果打开文件失败,函数会输出错误信息
原文地址: http://www.cveoy.top/t/topic/g1zt 著作权归作者所有。请勿转载和采集!