Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with file handling in c++

Newbie
 
Join Date: Sep 2007
Posts: 23
#1: Oct 6 '07
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.


Expand|Select|Wrap|Line Numbers
  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<process.h>
  6. class emp
  7. {
  8.     int id;
  9.     char name[20];
  10.     float sal;
  11. public:
  12.  
  13.     void getdata()
  14.     {
  15.          cout<<"Enter id:="<<"\n";
  16.          cin>>id;
  17.          cout<<"Enter name:="<<"\n";
  18.          cin>>name;
  19.          cout<<"Enter salary:="<<"\n";
  20.          cin>>sal;
  21.     }
  22.     void putdata()
  23.     {
  24.          cout<<"Id      :="<<"\t "<<id<<endl;
  25.          cout<<"Name    :="<<"\t "<<name<<endl;
  26.          cout<<"Salary  :="<<"\t "<<sal<<endl;
  27.     }
  28.     int getid()
  29.     {
  30.     return id;
  31.     }
  32.     void modify();
  33. }e1,emp1;
  34. void emp::modify()
  35. {
  36.     cout<<"Emp  id" <<id<<"\t";
  37.     cout<<"Emp  name" <<name<<"\t";
  38.     cout<<"Emp  Salary" <<sal<<"\n";
  39.     cout<<"Enter new details "<<endl;
  40.     char nm[20]=" ";
  41.     int sal1;
  42.     cout<<"New Name :(enter  '.' to retain old one)";
  43.     cin>>nm;
  44.     cout<<"New Salary :(enter  '-1' to retain old one)";
  45.     cin>>sal1;
  46.     if(strcmp(nm,".")!=0)
  47.         strcpy(name,nm);
  48.     if(sal1!=-1)
  49.         sal=sal1;
  50. }
  51.  
  52. void main()
  53. {
  54.     long pos;
  55.     char found='f';
  56.     int id1;
  57.     clrscr();
  58.     fstream fio;
  59.     fio.open("emp.dat",ios::app|ios::out|ios::binary);
  60.     emp1.getdata();
  61.     fio.write((char *)&emp1,sizeof (emp1));
  62.     cout<<"The contents before modification \n";
  63.     while(!fio.eof())
  64.     {
  65.         fio.read((char *)&emp1,sizeof (emp1));
  66.         emp1.putdata();
  67.     }
  68.     cout<<"Enter id of an Emploee whose record is to be modified "<<endl;
  69.     cin>>id1;
  70.     fio.seekg(0);
  71.  
  72.     while(fio)
  73.     {
  74.         pos=fio.tellg();
  75.         fio.read((char *)&e1,sizeof (e1));
  76.  
  77.         if(e1.getid()==id1)
  78.         {
  79.             e1.modify();
  80.             fio.seekg(pos);
  81.             fio.write((char *)&e1,sizeof(e1));
  82.             found='t';
  83.             break;
  84.         }
  85.     }
  86.     if(found=='f')
  87.         cout<<"Record not found !!\n";
  88.     fio.seekg(0);
  89.     cout<<"Now the contents \n";
  90.     while(!fio.eof())
  91.     {
  92.         fio.read((char *)&emp1,sizeof (emp1));
  93.         emp1.putdata();
  94.     }
  95.     fio.close();
  96. getch();
  97. }

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 6 '07

re: Problem with file handling in c++


I didn't exactly understand the problems you were having - could you please explain them in better detail?
Newbie
 
Join Date: Sep 2007
Posts: 23
#3: Oct 6 '07

re: Problem with file handling in c++


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?
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Oct 7 '07

re: Problem with file handling in c++


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.
Newbie
 
Join Date: Sep 2007
Posts: 23
#5: Oct 9 '07

re: Problem with file handling in c++


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.Please help me with code.I need it badly.
Newbie
 
Join Date: Oct 2007
Posts: 4
#6: Oct 26 '07

re: Problem with file handling in c++


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.
Newbie
 
Join Date: Jun 2007
Posts: 15
#7: Oct 27 '07

re: Problem with file handling in c++


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

Your loop says:

Expand|Select|Wrap|Line Numbers
  1.   while there is no file failure (eof):
  2.     abc <-- read next record
  3.     display abc
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. while (true)
  2. {
  3.   if (! file.read( ... )) break;
  4.   abc.showdetails();
  5.   cout <<endl;
  6. }
  7.  
Hope this helps.
Newbie
 
Join Date: Oct 2007
Posts: 4
#8: Dec 20 '07

re: Problem with file handling in c++


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
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#9: Dec 20 '07

re: Problem with file handling in c++


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.
Reply