C++txt文件读取和写入

//写

#include

#include
using namespace std;

int main()
{
    string id,name;
    double grade;
    ofstream write;
    write.open("E:\\practice\\程序设计B\\ttt.txt",ios::in);

    if(!write)
    {
        cout << "can not" << endl;

        abort();
    }

    write << "学生管理" << endl;
    cout << "输入id,name,grade" << endl;
    while(cin >> id >> name >> grade)
    {
        write << id << ' ' << name << ' ' << grade << endl;
    }

    write.close();
}

//读

#include

#include
using namespace std;

int main()
{
    char s[100];
    string id,name;
    double grade;

    ifstream read;
    read.open("E:\\practice\\程序设计B\\ttt.txt",ios::out);

    read.seekg(0,ios::beg);

    if(!read)
    {
        cout << "文件打开失败!" << endl;
        abort();
    }

    read.getline(s,100);

    while(read >> id >> name >> grade)
    {
        cout << id << " " << name << " " << grade << endl;
    }

    read.close();
}

你可能感兴趣的:(c++)