如何写入随机访问文件

How do I write to a random access file?

本文关键字:文件 访问 随机 何写入      更新时间:2023-10-16

此代码应该将一些项目写入文件,但创建的文件具有 0 个字节并且为空。我真的不确定有什么问题。

我没有任何错误。另外,随机访问文件可以.txt还是必须.dat?

add 函数运行并添加信息没有错误,但文件中没有任何内容。

void add()
{
int hour, min, day, month, yr, snum, interNo,age;
string fname, lname, clinic, area, ex, li, type, sname, town, pay,problem, breed, gender;
char ans;
Intervention inte;

fstream InfoFile("JSPCA.dat", ios::in | ios::out |ios::app);
if (!InfoFile)
{
    cout << "Error -file could not be opened.";
}
else
{
    cout<<"Enter intervention numbern";
    ("Enter 0 to end input)n");
    cin>>interNo;
    system("cls");

    InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
    /* read record from file*/
    InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));
    if (inte.getClient().getInterventionNo() != 0)
    {
        cout << "inter # already contains information.n";
    }
    else {
        /* create record */
        while (interNo != 0) {
            InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
            InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));

            if (inte.getClient().getInterventionNo() != 0)
                cout << "inter # already contains information.n";
            else {
                // user enters information, which is copied into file
                cout << "Enter name of clinic(Winchester or Caymanas)n ";
                cin >> clinic;
                cout << "Enter lastname, firstnamen ";
                cin >> lname;
                cin >> fname;
                cout << "Please Address(Street#, Street Name, Town)n";
                cin >> snum >> sname >> town;
                cout << "Enter Contact#:(area exchange line)n";
                cin >> area >> ex >> li;
                cout << "Enter Animal Typen";
                cin >> type;
                cout << "Enter Animal Breedn";
                cin >> breed;
                cout << "Enter animal's gendern";
                cin >> gender;
                cout << "Enter Animal problemn";
                cin >> problem;
                cout << "Enter age of animaln";
                cin >> age;
                cout << "Please enter letter for payment type:n";
                cout << "Full=Fn";
                cout << "Contribution=Cn";
                cout << "Can't Pay=CPn";
                cin >> pay;
                cout << "Enter date in format  dd mm yyyyn";
                cin >> day >> month >> yr;
                cout << "Enter the time(hour minuten)";
                cin >> hour >> min;
                //inte.getClient().getInterventionNo() == interNo;
                // set records for Client, address, TelNo, animal, date and time values
                inte.setClient(Client(fname, lname, pay, interNo, clinic));
                inte.setAddress(Address(snum, sname, town));
                inte.setTelNo(TelNo(area, ex, li));
                inte.setAnimal(Animal(type, breed, gender, problem, age));
                inte.setDate(Date(day, month, yr));
                inte.setTime(Time(hour, min));

                InfoFile.seekp(sizeof(Intervention)*(interNo - 1));
                InfoFile.write(reinterpret_cast<const char *>(&inte), sizeof(Intervention));
            }
            system("cls");
            cout << "Enter new intervention numbern";
            cout<<"Enter 0 to end input";
            cin >> interNo;
        }
        }
}
}

至少存在三个错误

1) 当你的对象包含 std::string 时,你不能做二进制 I/O。我猜是这种情况,您还没有发布您正在使用的对象的定义。底线是,你不应该只是假设你可以在任何东西上做二进制I/O。您必须了解可以使用二进制 I/O 输出哪种对象。我猜你的不能。

2)如果你想做二进制I/O,你必须用ios::binary打开文件

3)这就是您获得空文件的原因。如果文件为空(或不存在),则

InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));

将失败。在此之后,在清除错误(您永远不会这样做)之前,任何输入或输出操作都将起作用。因此,以下写入也会失败,最终会得到一个空文件。

我认为您在二进制和随机访问 I/O 方面有一些研究要做。这并不接近正确。