我的对象没有被存储在.dat文件中。

问题描述 投票:0回答:1

谁能告诉我这段代码中我做错了什么?当我在汽车类的对象中输入数据时,它正确地输入了,但它没有被存储在 "CARS.dat "文件中。

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class Car{
    public:
    string Model_NO, manufacturer, car_no;
    Car(){ manufacturer="DEY MOTORS"; }
    void Disp(){
        cout<<"Model_Number:-"<<Model_NO<<endl;
        cout<<"Car Number:-"<<car_no<<endl;
        cout<<"Manufacturer:-"<<manufacturer<<endl;
    }
};
Car Enter(){
    Car c1;
    cout<<"\nEnter Model_Number:-"; cin>>c1.Model_NO;
    cout<<"Enter Car Number:-"; cin>>c1.car_no; return c1;
}
int main(){
    Car *c; int n; char d;
    fstream file;
    cout<<"What do you want:-"<<endl<<"1. Enter"<<endl<<"2. Show"<<endl;
    d=getch();
    if(d=='1'){
        cout<<"Enter Number:-"; cin>>n;
        for(int i=0;i<n;i++){
            c=new Car(); (*c)=Enter(); c->Disp();
            file.open("CARS.dat",ios::app|ios::binary);
            file.write((char*)c , sizeof(*c)); delete c;
        }
    }else if(d=='2'){
        file.open("CARS.dat",ios::in|ios::binary);
        while(!file.eof()){
            c=new Car();
            file.read((char*)c , sizeof(*c));
            c->Disp(); delete c;
        }
    }
    return 0;
}
c++ file-handling
1个回答
1
投票

在...

for(int i=0;i<n;i++){
    c=new Car(); (*c)=Enter(); c->Disp();
    file.open("CARS.dat",ios::app|ios::binary);
    file.write((char*)c , sizeof(*c)); delete c;
}

file.open("CARS.dat",ios::app|ios::binary); 打开文件。没有任何东西可以关闭它。如果你打开了一个打开的文件流,该文件流会进入错误状态,并变得不可用,直到该错误被承认,并且 cleared.

第一次打开文件,大概就能用了。因为没有进行检查,所以无法确定它是否有效。在尝试使用事务结果之前,一定要确认一个IO事务成功。

注意:为了防止答案铺张,我在这里不处理正确的序列化问题。这在其他地方都有涉及。很多地方都有。

if(d=='1')
{
    ofstream file("CARS.dat",ios::app|ios::binary); //allocate and open file
    if (file.is_open())
    { 
        cout<<"Enter Number:-"; cin>>n;
        for(int i=0;i<n;i++){
            car c = Enter(); // no need for dynamic allocation here. 
            c.Disp();
            // serialize c to file here.
            if (!(file))
            { // failed to write to file. 
                // handle error here as is appropriate for the program.
                // the following notifies the user and then exits the program. 
                std::cerr << "Error writing Cars.dat\n";
                return -1;
            } 
        } // car c destroyed here free of charge
    }
    else
    {
        std::cerr << "could not open Cars.dat\n";
    }  
} // file closed and destroyed here.
else if(d=='2')
{
    ifstream file("CARS.dat"); //allocate and open file for reading
    if (file.is_open())
    {
        while(true) // loop FOREVER!!!! Muhuhahahahahahahaha!!!!!!
        {
            Car c;
            //deserialize file into c
            if (file) // read a Car
            {
                c.Disp(); // display it.
            }
            else
            {
                break; // OK. Not forever. Exit when we can't read a Car
            }
        } 
    }
    else
    {
        std::cerr << "could not open Cars.dat\n";
    }  
}
© www.soinside.com 2019 - 2024. All rights reserved.