2023 5 18

xuxingkai / 2023-05-18 / 原文

#include <iostream>
#include <fstream>
using namespace std;
class Tercher
{
public:
    string name = "";
    int ID = 0;
    char sex = 'm';
};
string name;
int ID;
char sex;
int main()
{
    ofstream ofs("D:\\VisualStudio2022\\teacher.dat", ios::out | ios::binary);
    if (!ofs)
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    int n;
    cout << "输入教师数量:";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        Tercher t;
        cin >> name >> ID >> sex;
        t = { name,ID,sex };
        ofs.write((const char*)&t, sizeof(t));
    }
    ofs.close();
    ifstream ifs("D:\\VisualStudio2022\\teacher.dat", ios::in | ios::binary);
    if (!ifs.is_open())
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    Tercher t;
    for (int i = 0; i < n; i++)
    {
        ifs.read((char*)&t, sizeof(t));
        cout << "名字:" << t.name << " " << "工号:" << t.ID << " " << "性别:" << sex << endl;
    }
    ifs.close();
    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream ofs;
    ofs.open("D:\\VisualStudio2022\\M99.txt", ios::out);
    if (!ofs)
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
            ofs << j << " * " << i << " = " << j * i << " ";
        ofs << endl;
    }
    ofs.close();
    ifstream ifs;
    ifs.open("D:\\VisualStudio2022\\M99.txt", ios::in);
    if (!ifs)
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string word;
int main()
{
    ofstream ofs;
    ofs.open("D:\\VisualStudio2022\\Word.txt", ios::out);
    if (!ofs)
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    getline(cin, word);
    ofs << word;
    ofs.close();
    ifstream ifs;
    ifs.open("D:\\VisualStudio2022\\Word.txt", ios::in);
    if (!ifs)
    {
        cout << "文件打开失败" << endl;
        return 0;
    }
    int ans = 0;
    char c;
    while ((c = ifs.get())!=EOF)
    {
        if (c == ' ')
            ans++;
    }
    ifs.close();
    cout << "单词数量为:" << ans+1 << endl;
    return 0;
}

char buff[1024];
    while (ifs.getline(buff, sizeof(buff)))
    {
        cout << buff << endl;
    }
    ifs.close();
    return 0;
}