#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdexcept>
#include<algorithm>
//1 引入头文件
#include<fstream>
#include<string>
//把磁盘信息输入到缓冲区 然后读到程序中(读文件)
void test02() {
std::ifstream ifs;
ifs.open("test.txt", std::ios::in);
if (ifs.is_open() == false) {
std::cout << "open fail\n";
}
//std::string s;
//while (ifs >> s) {
// std::cout << s << '\n';
//}
char buf[1024]{};
while (!ifs.eof()) {
ifs.getline(buf, sizeof buf);
std::cout << buf << '\n';
}
//close file
ifs.close();
}
auto main()->int32_t{
auto test = [&]()->void {
//2. define stream object
std::ofstream ofs;
//3 open file with write if no file then generate
ofs.open("test.txt", std::ios::out | std::ios::trunc);
//4 check open
if (!ofs.is_open()) {
std::cout << "open fail\n";
}
//5 write message
ofs << "name:\n";
ofs << "age:\n";
ofs << "tall:\n";
//6 close file
ofs.close();
};
test02();
return static_cast<int>(0);
}
标签:std,文件,cout,读写,ios,c++,test,include,open
From: https://www.cnblogs.com/lambdaios/p/17966641