c++ - 如何写入随机访问文件?

标签 c++ filestream

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

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

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

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) 当您的对象包含 std::string 时,您不能进行二进制 I/O。我猜是这种情况,你还没有发布你正在使用的对象的定义。底线是你不应该只是假设你可以对任何东西进行二进制 I/O。您必须了解使用二进制 I/O 可以输出什么样的对象。我猜你的不能。

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

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

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

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

我认为您需要学习二进制和随机访问 I/O。这不太正确。

关于c++ - 如何写入随机访问文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894882/

相关文章:

c++ - 包含类型的类型的模板函数重载

c++ - 使用 SSE 或 SSE3 在 ushort 数组中添加 uchar 值

C++缓存文件是什么意思?

c++ - 为什么 make_optional 不适用于文件流?

c++ - 我怎样才能在可滚动的 QTableWidget 中有一个卡住的水平标题?

c++ - 更短的循环,相同的覆盖范围,为什么我在使用 Visual Studio 2013 的 C++ 中得到更多的末级缓存未命中?

android - 在华硕 Zenfone AR 上使用 Tango3DR_AreaDescription_createFromDataset

c++ - basic_istream<>::tellg() 和 basic_ostream<>::tellp() 的区别

perl - 刷新行阅读运算符 Perl

Android-保存文件(输入和输出流)抛出异常(打开失败 : EINVAL (Invalid argument)) in Pre-lollipop devices