写文件
#include<iostream> using namespace std; #include<fstream> int main(void) { //1.包含头文件 fstream //2.创建流对象 ofstream ofs; //3.指定打开文件 ofs.open("text.txt", ios::out); //4.写内容 ofs << "姓名:张三" << endl; ofs << "性别:男" << endl; ofs << "公司:xxx无限公司" << endl; //5.关闭文件 ofs.close(); return 0; }
//读文件
#include<iostream> using namespace std; #include<fstream> #include<string> int main(void) { ifstream ifs; ifs.open("text.txt", ios::in); string buf; while (getline(ifs, buf)) { cout << buf << endl; } ifs.close(); return 0; }

计算机小白记录学习过程,喜欢就点个推荐和关注吧O(∩_∩)O哈哈~

浙公网安备 33010602011771号