开发者

file manipulation

in c++ i have written a simple program, which accepts 4 to 6 records and then do not accept any more when airline_no is same i.e. 1? The source code is:

#include<fstream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdlib.h>
#include<process.h>
#include<stdio.h>
int lno;
struct airln {int airline_no,routeno,dep_time,arr_time;
    char port_d[15],port_a[15],week_day[10];  }r2;

class route
{
 public:
    void getroute()
    {

        cout<<"\n\tRoute no.: ";
        cin>>r2.routeno;cout<<"\n";
        cout<<"\tDay of Depa开发者_如何学运维rture: ";
        gets(r2.week_day);cout<<"\n";
                cout<<"\tAirport for departure: ";
        gets(r2.port_d);cout<<"\t";
        cout<<"Departure Time: ";
        cin>>r2.dep_time;cout<<"\n";
        cout<<"\tAirport for arrival: ";
        gets(r2.port_a);cout<<"\t";         
                cout<<"Arrival Time: ";
        cin>>r2.arr_time;cout<<"\n";
        cout<<"\n\tAirline no.: ";
        cin>>r2.airline_no;
    }
    void display_route()
    {
    cout<<"\n    Route No : ";cout<<r2.routeno;
    cout<<"\n    ";
    cout<<r2.port_d;cout<<"\t";
    cout<<r2.dep_time;cout<<"\t\t";
    cout<<r2.port_a;cout<<"     \t";
    cout<<r2.arr_time;cout<<"\t\t";
    cout<<r2.week_day;cout<<"\n";
    }
}r3;
void main()
{
    clrscr();
    int airlnno,rtnodel,cntr;
    char airlinename[30];
    ifstream fin,fin1;
    ofstream fout;

    do
    {
    cout<<"\t1. Insert Data.\n";
    cout<<"\t2. View Data.\n";
    cout<<"\t3. Exit.\n";
    cout>>"\tEnter Choice : ";cin>>cntr;
    switch(cntr)
    {

           case 1:r3.getroute();
                   fout.open("testdata.dat",ios::app);
                   if(!fout)
                   {
                        gotoxy(25,10);
                        cout<<"No file exists or file can\'t be opened\n";
                        gotoxy(25,22);
                        cout<<"Please Press Any Key to Continue.......";
                        getch();
                        clrscr();
                       }
                   fout.write((char *) &r2,sizeof(r2));
                   fout.close();
                   clrscr();
                   break;
        case 2:fin.open("testdata.dat",ios::in);
               if(!fin)
               {
                gotoxy(25,20);
                cout<<"No file exists or file can\'t be opened\n";
                gotoxy(25,22);
                cout<<"Please Press Any Key to Continue.......";
                getch();
                clrscr();
                break;
               }
               cout<<"\n    Dep-Airport\t";
               cout<<"Dep-Time\t";
               cout<<"Arr-Airport\t";
               cout<<"Arr-Time\t";
               cout<<"Week Day\n";
               fin.read((char *) &r2,sizeof(r2));
               while(!fin.eof())
               {
            r3.display_route();
            fin.read((char *) &r2,sizeof(r2));
               }
               fin.close();
               gotoxy(25,22);
               cout<<"Please Press Any Key to Continue.......";
               getch();
               clrscr();
               }
               } while(!(cntr==3));
}


You need to clear cin after receiving each input each time. Probably at the begining of do while loop. cntr value may be taken from previously entered values. If it happens to be 3, it exits while loop and does not accept any further records.

Try out cin.clear() or cin.ignore() functions at the begining of do while loop.

You can refer How do I flush the cin buffer?


Do you have to use files for this? You could always use std::map and keep the airline_no as the key.

I'd also avoid using the global struct and class.

Edit: k, I've got a problem and found that the 1050 is the problem. I can move it to an earlier record and it will stop there too. I think the problem is that 1050 is somehow triggering End of File. Try the following replacement lines to open the file in binary:

fout.open("testdata.dat",ios::app | ios::binary);
fin.open("testdata.dat",ios::in | ios::binary);

instead of:

fout.open("testdata.dat",ios::app);
fin.open("testdata.dat",ios::in);

Edit: Just had a check and 1050 converts to 041A, which translates to the characters End of Transmission and then.... SUB or EOF. I think that's your problem and in binary mode this shouldn't be a problem any more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜