如何检测和删除文件处理代码中的运行时错误?

时间:2018-03-16 05:40:54

标签: c++ runtime-error

问:为什么这段代码不能正常运行?

我用C ++语言实现了文件处理。

我已经创建了一个完整的汽车文件。通过此代码,我们可以存储新数据,删除旧数据,搜索有关汽车的特定数据。问题是此代码成功编译但在运行时,其执行停止并显示错误消息。我请求帮助我从此代码中删除运行时错误。

CODE:

#include<iostream>
#include<string.h>
#include<conio.h>
#include<fstream>
using namespace std;

struct car
{
    char name[20];
    int  model;
    string color;
    int car_id;
    string size;
    float weight;
    int price;
};

void getdata(car&);         //function declaration
void showdata(car&);        //function declaration
void searchdata();          //function declaration
void deleterecord();        //function declaration
void modify();              //function declaration
void readdata();            //function declaration
void writedata();           //function declaration

int main()
{
    char ch;
    cout<<"\nEnter w to write record ";
    cout<<"\nEnter r to read record";
    cout<<"\nEnter m to modify record";
    cout<<"\nEnter s to search record";
    cout<<"\nEnter d to delete record";
    cout<<"\n\nEnter your choice :";
    cin>>ch;

    switch(ch)
    {
        case 'w':
        {
            writedata();
            break;
        }
        case 'r':
        {
            readdata();
            break;
        }
        case 's':
        {
            searchdata();
            break;
        }
        case 'd':
        {
            deleterecord();
            break;
        }

        case 'm':
        {
            modify();
            break;
        }
        default:
        {
            cout<<"\nWrong choice";
        }
    }
    return 0;
}

void getdata(car &ccc)
{
    cout<<"Please enter name of car :";
    cin>>ccc.name;
    cout<<"Please enter model number of car:";
    cin>>ccc.model;
    cout<<"Enter color of car:";
    cin>>ccc.color;
    cout<<"Enter id number of car:";
    cin>>ccc.car_id;
    cout<<"Enter size of car :";
    cin>>ccc.size;
    cout<<"Enter weight of a car :";
    cin>>ccc.weight;
    cout<<"Enter price of a car :";
    cin>>ccc.price;
}

void showdata(car &ccc)
{
    cout<<"\nName of car is :";
    puts(ccc.name);
    cout<<"\nModel number of car is :"<<ccc.model;
    cout<<"\nColor of car is :"<<ccc.color;
    cout<<"\nID number of car is :"<<ccc.car_id;
    cout<<"\nSize of car is :"<<ccc.size;
    cout<<"\nWeight of car is :"<<ccc.weight;
    cout<<"\nPrice of car is :"<<ccc.price;
}

void writedata()
{
    ofstream file;
    char ch='y';
    car ccc;
    file.open("carinformation.dat",ios::binary | ios::out | ios::app);
    while(ch=='y'|| ch=='Y')
    {
        getdata(ccc);
        file.write((char*)&ccc,sizeof(ccc));
        cout<<"\nDo you want to countine?";
        cin>>ch;
    }
    file.close();
}

void readdata()
{
    int count=0;
    ifstream file;
    car ccc;
    file.open("carinformation.dat",ios::binary | ios::in);
    if(!file)
    {
        cout<<"File not found";
        exit(0);
    }
    else
    {
        file.read((char*)&ccc,sizeof(ccc));
        while(!file.eof())
        {
            showdata(ccc);
            file.read((char*)&ccc,sizeof(ccc));
            count++;
        }
        cout<<"Number of records are :"<<count;
    }
    file.close();
}

void searchdata()
{
    car ccc;
    ifstream file;
    char n_c[20];
    cout<<"Enter name of car :";
    cin>>n_c;
    file.open("carinformation.dat",ios::binary | ios::in);
    if(!file)
    {
        cout<<"File nnot found";
        exit(0);
    }
    else
    {
        file.read((char*)&ccc, sizeof(ccc));
        while(!file.eof())
        {
            if(strcmp(n_c,ccc.name)==0)
            {
                showdata(ccc);

            }
            file.read((char*)&ccc, sizeof(ccc));
        }
    }
    file.close();
}

void modify()
{
    car ccc;
    fstream file;
    char n_c[20];
    file.open("carinformation.dat",ios::binary | ios::in | ios::out);
    cout<<"\nEnter name of car that should be searched:";
    cin>>n_c;
    if(!file)
    {
        cout<<"File not found";
        exit(0);
    }
    else
    {
        file.read((char*)&ccc,sizeof(ccc));
        while(!file.eof())
        {
            if(strcmp(n_c,ccc.name)==0)
            {
                file.seekg(0, ios::cur);
                cout<<"Enter new record :\n";
                getdata(ccc);
                int i=file.tellg();
                int j=sizeof(ccc);
                int k=i-j;
                file.seekp(k);
                file.write((char*)&ccc, sizeof(ccc));
            }

        }
    }
    file.read((char*)&ccc, sizeof(ccc));
    file.close();
}
void deleterecord()
{
    int count=0;
    car ccc;
    int c_id;
    cout<<"Please enter car id :";
    cin>>c_id;
    ifstream file;
    file.open("carinformation.dat" ,ios::binary| ios::in);
    ofstream file2;
    file2.open("New carinformation.dat", ios::binary| ios::out);
    while(file.read((char*)&ccc,sizeof(ccc)))
    {
        if(ccc.car_id!=c_id)
        {
            file2.write((char*)&ccc ,sizeof(ccc));
            count++;
        }
    }
    cout<<"Number of records are :"<<count;
    file2.close();
    count=0;
    file2.open("New carinformation.dat", ios::binary| ios::in);
    file.read((char*)&ccc,sizeof(ccc));
    while(!file.eof())
    {
        count++;
        showdata(ccc);
        file.read((char*)&ccc,sizeof(ccc));
    }
    cout<<"Number of records are :"<<count;
    file.close();
    file2.close();
    remove("carinfomation.dat");
    rename("New carinformation.dat", "carinformation.dat");
    file.open("carinformation.dat", ios::binary  | ios::in);
    file.read((char*)&ccc, sizeof(ccc));
    while(!file.eof())
    {
        count++;
        showdata(ccc);
        file.read((char*)&ccc, sizeof(ccc));
    }
    cout<<"Number of records are :"<<count;
}

2 个答案:

答案 0 :(得分:2)

car具有std::string个字段,这些字段保存指向堆分配内存的指针。因此,通过写入原始字节file.write((char*)&ccc,sizeof(ccc));将其保存到文件中是行不通的。而且,更重要的是,稍后阅读file.read((char*)&ccc, sizeof(ccc));将使用无效指针值填充字符串对象。您需要逐个存储字段并逐个仔细地读取输入数据。

答案 1 :(得分:0)

“string”和“string.h”之间存在差异。删除两个字符“.h”并查看您的代码是否有效。

<string.h> contains functions like strcpy, strlen for C style null-terminated strings.
<string> contains std::string, std::wstring plus other classes.

你需要初始化你的结构,这是你的readdata()的一个例子,查看更改并找出如何修复程序的其余部分:

void readdata()
{
    int count=0;
    ifstream file;
    car *ccc = new car();
    file.open("carinformation.dat",ios::binary | ios::in);
    if(!file)
    {
        cout<<"File not found";
        exit(0);
    }
    else
    {
        file.read((char*)ccc,sizeof(*ccc));
        while(!file.eof())
        {
            showdata(*ccc);
            file.read((char*)ccc,sizeof(*ccc));
            count++;
        }
        cout<<"Number of records are :"<<count;
    }
    file.close();
}