如何写入随机访问文件?

时间:2013-11-10 20:37:53

标签: c++ filestream

此代码应该将一些项写入文件,但创建的文件有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 number\n";
    ("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, firstname\n ";
                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 Type\n";
                cin >> type;
                cout << "Enter Animal Breed\n";
                cin >> breed;
                cout << "Enter animal's gender\n";
                cin >> gender;
                cout << "Enter Animal problem\n";
                cin >> problem;
                cout << "Enter age of animal\n";
                cin >> age;
                cout << "Please enter letter for payment type:\n";
                cout << "Full=F\n";
                cout << "Contribution=C\n";
                cout << "Can't Pay=CP\n";
                cin >> pay;
                cout << "Enter date in format  dd mm yyyy\n";
                cin >> day >> month >> yr;
                cout << "Enter the time(hour minute\n)";
                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 number\n";
            cout<<"Enter 0 to end input";
            cin >> interNo;

        }
        }
}

}

1 个答案:

答案 0 :(得分:2)

至少有三个错误

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的研究。这不是正确的。