搜索二进制文件的特定位置以开始读取或写入

时间:2018-01-20 03:59:40

标签: c++

假设您有一个包含多个结构类型数据条目的数据文件

根据其中一个数据条目的代码编号变量,您应该绘制结构的其余变量。

或者我应该尝试根据scode找到记录号码? 然后使用记录号找到我需要从头开始使用seekg跳过的大小

char scode[MAX];
Subject M;

afile.open (fileName, ios::in | ios::out | ios::binary);

cout << "Enter Subject code: ";
    cin >> scode;
    cin.clear();
    cin.ignore(100,'\n');
    cout << endl << endl;
    while (afile.read (reinterpret_cast <char *>(&M), sizeof (M)))
        {
            if (strcmp(scode,M.subCode) == 1)
            {
                cout << "Subject code not found" << endl;
                cout << "----------------------------" << endl;
                return;
            }

            else
            {
                afile.seekg(-strlen(M.subCode),ios::cur);
                cout << "Subject Code: " << M.subCode << endl
                    << "Subject Name: " << M.subTitle << endl;

                cout << left << setw(8) << "Task"
                     << left << setw(14) << "Title"
                     << right << setw(7) << "Weight"
                     << right << setw(7) << "Upon"
                     << right << setw(7) << "Mark"
                     << right << setw(12) << "Obtained"
                     << endl << endl;

                cout << setw (66) << setfill ('-') << "-" << endl;
                cout << setfill (' ');

                while (afile.read (reinterpret_cast <char *>(&M), sizeof (M)))
                {
                    for (int i = 1; i <= M.noTask; i++)
                    {
                        cout << left << setw(8) << i
                             << left << setw(14) << M.Task[i].title
                             << right << setw(7) << M.Task[i].weight
                             << right << setw(7) << M.Task[i].fullmark
                             << right << setw(7) << M.Task[i].mark
                             << right << setw(12) << M.Task[i].mark / M.Task[i].fullmark * M.Task[i].weight
                             << endl;
                    }
                }
            }

0 个答案:

没有答案