写文件

#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; }

 

posted @ 2021-01-12 18:52  loliconsk  阅读(47)  评论(0)    收藏  举报