C++ 在我的简单c++;代码

C++ 在我的简单c++;代码,c++,C++,这是我的代码: #include <iostream> #include <fstream> #include <string> using namespace std; struct car { string name, model; int year; }; void search_car(int CarYear) { cout<<"1"; ifstream in; cout<<

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

struct car
{
    string name, model;
    int year;   
};

void search_car(int CarYear)
{
    cout<<"1";
    ifstream in;
    cout<<"2";
    car c1;
    cout<<"3";
    in.open("Cars.txt",ios::binary|ios::in);
    cout<<"4"<<endl;
    while(!in.eof())
    {
        cout<<" 5";
        in.read((char *) &c1, sizeof(car));
        cout<<" 6.Car Year: "<<c1.year<<endl;
        if(c1.year == CarYear)
        {
            cout<<" 7>>> ";
            cout<<c1.name<<" "<<c1.model<<" "<<c1.year;
            cout<<" <<<8"<<endl;
        }
    }
    cout<<" 9";
    in.close();
    cout<<" 10";    
}

void main()
{
    car c[100];
    int carNum, menuAct = 0, CarYear = -1, cycle = 1;
    ofstream out;
    while (cycle == 1)
    {
        //clrscr();
        cout<<endl<<endl<<"1.Enter New car"<<endl<<"2.Search"<<endl<<"3.Exit"<<endl;
        cin>>menuAct;
        cout<<"   Menu Action: "<<menuAct<<endl;
        if(menuAct == 1)
        {
            cout<<"Enter Num OF Cars: ";
            cin>>carNum;
            out.open("Cars.txt",ios::binary|ios::out|ios::app);
            for(int i = 0; i < carNum; i++)
            {
                cout<<"Enter Name OF Car: ";
                cin>>c[i].name;
                cout<<"Enter model OF Car: ";
                cin>>c[i].model;
                cout<<"Enter year OF Car: ";
                cin>>c[i].year;     
                out.write((char *) &c[i], sizeof(car));
            }
            out.close();
        }
        else if(menuAct == 2)
        {
            cout<<"Enter Car Year: ";
            cin>>CarYear;
            cout<<" 0";
            //cout<<" Y: "<<CarYear;
            search_car(CarYear);
            cout<<" 11";
            //menuAct = 0;
        }
        else if(menuAct == 3)
        {
            cycle = 0;
        }
    }   
}
#包括
#包括
#包括
使用名称空间std;
结构车
{
字符串名称、模型;
国际年;
};
无效搜索车(整年)
{

cout我并不奇怪您会遇到问题!您正在按字面意思保存结构的字节,然后当您从文件中读回它们时,您希望能够再次获得std::string。这样根本不起作用

问题是car结构没有包含它引用的所有数据:std::string成员实际上只是指向包含实际字符串数据的动态数组的指针。您正在将car结构作为原始字节写入,因此字符串永远不会被归档。它们不可能从中读回

更糟糕的是,当您读回结构时,您正在将std::string中的指针设置为垃圾值。您不可能希望它们恰好指向的内存包含您想要的内容

您需要为car结构定义序列化函数,使用深度副本将其发送到扩展流,并安全地将其读回。切勿将原始指针值写入文件

示例代码
ostream和运营商c.年;
回报是;
}
在.read((char*)和c1中更改
,大小为(car))到>>c1中的


更改
out.write((char*)&c[i],sizeof(car))到<代码>首先,在任何情况下都不能使用void main,main总是返回int类型。有人能帮我吗?我的问题有什么缺点?!谢谢你需要睡觉。在我的代码中是否有任何其他逻辑。。。错误?我建议你把它缩小到一个小的,简单的,能够复制错误的独立程序。没有人想调试别人的代码墙。eof循环会给你带来一些麻烦,这就是为什么你的最后一辆车会被打印两次。下面是我写的一个常用的适当EOF循环的例子
ostream& operator <<(ostream& os, const car& c) {
    return os << c.name << endl << c.model << endl << c.year << endl;
}
istream& operator >>(istream& is, car& c) {
    is >> c.name;
    is >> c.model;
    is >> c.year;
    return is;
}