Problem with file handling in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashwini1680
    New Member
    • Sep 2007
    • 23

    Problem with file handling in c++

    I have a problem working with files in c++.This is my code for modifying a record in a file.When i execute it ,it doesn't check rec in file & comes out .What's wrong???
    Many time working with file(adding record &then printing on screen or after deletion print the rec )last rec is read twice or otherwise each rec ie read twice I have checked my code.Still it's not working Pls help me If u can give correct code it will help me.


    [Code=cpp]
    #include<fstrea m.h>
    #include<conio. h>
    #include<stdio. h>
    #include<string .h>
    #include<proces s.h>
    class emp
    {
    int id;
    char name[20];
    float sal;
    public:

    void getdata()
    {
    cout<<"Enter id:="<<"\n";
    cin>>id;
    cout<<"Enter name:="<<"\n";
    cin>>name;
    cout<<"Enter salary:="<<"\n" ;
    cin>>sal;
    }
    void putdata()
    {
    cout<<"Id :="<<"\t "<<id<<endl ;
    cout<<"Name :="<<"\t "<<name<<en dl;
    cout<<"Salary :="<<"\t "<<sal<<end l;
    }
    int getid()
    {
    return id;
    }
    void modify();
    }e1,emp1;
    void emp::modify()
    {
    cout<<"Emp id" <<id<<"\t";
    cout<<"Emp name" <<name<<"\t";
    cout<<"Emp Salary" <<sal<<"\n";
    cout<<"Enter new details "<<endl;
    char nm[20]=" ";
    int sal1;
    cout<<"New Name :(enter '.' to retain old one)";
    cin>>nm;
    cout<<"New Salary :(enter '-1' to retain old one)";
    cin>>sal1;
    if(strcmp(nm,". ")!=0)
    strcpy(name,nm) ;
    if(sal1!=-1)
    sal=sal1;
    }

    void main()
    {
    long pos;
    char found='f';
    int id1;
    clrscr();
    fstream fio;
    fio.open("emp.d at",ios::app|io s::out|ios::bin ary);
    emp1.getdata();
    fio.write((char *)&emp1,sizeof (emp1));
    cout<<"The contents before modification \n";
    while(!fio.eof( ))
    {
    fio.read((char *)&emp1,sizeof (emp1));
    emp1.putdata();
    }
    cout<<"Enter id of an Emploee whose record is to be modified "<<endl;
    cin>>id1;
    fio.seekg(0);

    while(fio)
    {
    pos=fio.tellg() ;
    fio.read((char *)&e1,sizeof (e1));

    if(e1.getid()== id1)
    {
    e1.modify();
    fio.seekg(pos);
    fio.write((char *)&e1,sizeof(e1 ));
    found='t';
    break;
    }
    }
    if(found=='f')
    cout<<"Record not found !!\n";
    fio.seekg(0);
    cout<<"Now the contents \n";
    while(!fio.eof( ))
    {
    fio.read((char *)&emp1,sizeof (emp1));
    emp1.putdata();
    }
    fio.close();
    getch();
    }[/code]
    Last edited by Ganon11; Oct 6 '07, 09:07 PM. Reason: Fixing [CODE] tags.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I didn't exactly understand the problems you were having - could you please explain them in better detail?

    Comment

    • ashwini1680
      New Member
      • Sep 2007
      • 23

      #3
      The code is for modyfying a record in an existing file
      So i have taken emp id from user & then that id is searched in a file.If it matches with the id of any re in file whole rec is modified.But when i wrote loop for traversing throug rec in file it shows rec not found.I traced my program n i found that it doesn't go inside the loop even if rec are present in file
      I hope my question is clear now n i will get solution for it.
      Also while printing rec from files last rec is repeated .why?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I don't know what to tell you - it looks like you are using legacy C style functions with seekg and tellg, etc. I normally use the subclasses ifstream and ofstream for my file work - they function just like cin and cout, respectively, but with files.

        Comment

        • ashwini1680
          New Member
          • Sep 2007
          • 23

          #5
          i have also used ifstream & ofstream.But in case of modification we have to write the new rec in position where the old rec was.That's why before reading every rec its position in file(using tellg())in terms of byteno is taken & then file pointer is moved at that byte no(using seekg()) & the rec is written in file using write fun.That's what the logic is I don't understand what's wrong in it.In book also the program is written like this?If this willnot work then what's the alternative.Ple ase help me with code.I need it badly.

          Comment

          • ymprogenius
            New Member
            • Oct 2007
            • 2

            #6
            according to me you shud use "input" mode instead of "append" mode for openning the file because when it checks for end of file it is already in end due to append mode.

            Comment

            • tracethepath
              New Member
              • Jun 2007
              • 15

              #7
              i can help u with your problem that the last record repeats twice.

              Your loop says:

              Code:
                while there is no file failure (eof):
                  abc <-- read next record
                  display abc
              What happens is this:
              Read record one (no file read failure), display record one.
              Read record two (no file read failure), display record two.
              ...
              Read record N (no file read failure), display record N.
              Read record N+1 (couldn't: file read failure), display record N (since abc == last record)
              While loop terminates.

              You need to test the file failure before displaying the record. There are a zillion ways to rewrite this, so I offer my way:

              Code:
              while (true)
              {
                if (! file.read( ... )) break;
                abc.showdetails();
                cout <<endl;
              }
              Hope this helps.

              Comment

              • ymprogenius
                New Member
                • Oct 2007
                • 2

                #8
                Hav a look at my code actually its not written specially for u its a part of my banking project
                ignore cprintf () ,gotoxy() ,box() ,mainmenu() and textbackground( )etc from this program,coz they are just for presentation.

                here is the code

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  ymprogenius;

                  You have done what is called 'thread hijacking' - stealing someone else's thread to ask your own, unrelated question. In addition, you have failed to clearly outline what problem you have, instead merely saying "check my code." We're not your compiler, searching through code to find errors with no leads as to what might be wrong; you need to let us know what problems you are experiencing and what output you are expecting so we can better help you.

                  All this and more is found in our FAQ/Posting Guidelines, found under the 'Help' link at the top of your screen. Please read through these before re-posting your question in a new thread.

                  Comment

                  Working...