strcpy 正在复制 sth character onwards i string 。如何解决这个错误?

strcpy is copying sth character onwards i string . How to resolve this error?

我的完整代码在 pastebin.

有一个火车数据库,用户输入火车号来预订一个ticket.The功能updt_tick应该将火车名称,出发地和目的地的值复制到乘客的预订对象中。 但问题是第 5 个字符以后只被复制。 这是函数的代码 sample.Train 数据库由用户输入。

void updt_tick()
{
    fstream f;
    f.open("train.dat",ios::in | ios::binary);
    while(f.read((char*)&t,sizeof(t)))
    {
        if (tno==t.tno)
        {
            strcpy(bp,t.source);
            strcpy(dest,t.dest);
            strcpy(tname,t.tname);
            amt=SeatNum*t.PerSeatFare;
            break;
        }
    }
    f.close();
}

火车class是->

class train
{
public:
    int tno;
    char tname[100];
    char source[100];
    char dest[100];
    int PerSeatFare;

public:
    void getdetail();
    void showdetail();
}t;

预留 class 是 -> `

class reserv            
{
//Assume that cust select train according to his source and destination. 
public:
    int pnr;
    int tno;
    char tname[100];
    char pnames[10][100];
    int ages[10];
    int SeatNum;
    int i;
    int d,m,y;
    float amt;
    char bp[100],dest[100];
    void updt_tick()
    {
        fstream f;
        f.open("train.dat",ios::in | ios::binary);
        while(f.read((char*)&t,sizeof(t)))
        {
            if (tno==t.tno)
            {
                strcpy(bp,t.source);
                strcpy(dest,t.dest);
                strcpy(tname,t.tname);
                amt=SeatNum*t.PerSeatFare;
                break;
            }
        }
        f.close();
    }

public:
    void getresdet()
    {
        cout<<"Enter the details as follows\n";
        cout<<"Train no:";
        cin>>tno;
        cout<<"No of seats required:";
        cin>>SeatNum;
        cin.ignore();
        for(i=0; i<SeatNum ; i++)
        {
            cout<<"Passenger name:";
            gets(pnames[i]);
            cout<<"Passenger age:";
            cin>>ages[i];
            cin.ignore();
        }
        cout<<"Date of travel:";
        cin>>d>>m>>y;
        cout<<"Details Accepted\n";
        pnr=rand();
        updt_tick();
    }
    void showresdet()
    {
        cout<<"Pnr no:"<<pnr;
        cout<<"\nTrain no:"<<tno;
        cout<<"\nTrain name:";
        puts(tname);
        cout<<"Boarding point:";
        puts(bp);
        cout<<"Destination pt:";
        puts(dest);
        cout<<"No of seats reserved:"<<SeatNum;
        for(i=0; i<SeatNum; i++)
        {
            cout<<"Passenger name:";
            puts(pnames[i]);
            cout<<"Passenger age:"<<ages[i];
        }
        cout<<"\nDate of reservation:"<<d<<"-"<<m<<"-"<<y;
        cout<<"\nYou must pay:"<<amt<<endl;
    }
    int getpnr()
    {
        return pnr;
    }
};

Edit: There was nothing wrong with strcpy or any other code. I made the foolish mistake of giving the file name as "train.dat" instead of "trains.dat:.

我不确定你是怎么读完 5 个字符的,因为你的代码有问题。您用来存储火车详细信息的文件是 "trainS.dat",而不是 "train.dat"。