473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Last record of file is repeated twice while using ifstream for opening of file

//The out put of following code gives last record twice. What is wrong. Please help.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. struct abc
  8. {
  9.        char code[5],name[15],address[20];
  10.        int pay;
  11. };
  12. main()
  13. {
  14.  
  15.        abc rec;
  16.        char op;
  17.        int i=0;
  18.        ifstream rrr("employees.dat",ios::in | ios::binary);
  19.        if(!rrr)
  20.        {
  21.                   cerr<<("File opening error")<<endl;
  22.                   getche();
  23.                   exit(1); 
  24.        } 
  25.  
  26.        while(!rrr.eof()){
  27.  
  28.                   rrr.read((char*)&rec,sizeof(struct abc));
  29.  
  30.                   cout<<rec.code<<"\t"<<rec.name<<"\t"<<rec.address<<"\t"<<rec.pay<<endl;
  31.                        }
  32.        rrr.close();
  33.        getche();
  34.        return(0);
  35. }
Jun 2 '13 #1
2 1719
Oralloy
985 Expert 512MB
Hello Nazir,

This is pretty obviously a homework, so I'm going to help you find the problem.

Where the problem lies is in your process loop:
Expand|Select|Wrap|Line Numbers
  1.  
  2.        while(!rrr.eof()){
  3.  
  4.                   rrr.read((char*)&rec,sizeof(struct abc));
  5.  
  6.                   cout<<rec.code<<"\t"<<rec.name<<"\t"<<rec.address<<"\t"<<rec.pay<<endl;
  7.                        }
  8.  

First: When is EOF (End Of File) going to be detected?

HINT - your code is reading a fixed number of bytes from a binary file each time through the loop. What are the conditions that cause the EOF?

Second: How will you reconstruct the process loop to handle EOF, when it is detected?

HINT - what is the return value of the read method?

Good Luck!
Oralloy
Jun 2 '13 #2
First of all thank to Mr.Oralloy for help. Actually I am quite new person in learning c++ and have little background of programming. This exercise I copied from a book named "Programming with C++ (Aikman series)written by CM Aslam a pakistani author. I am sorry to say that I could not understand your reply. If you are kind enough to help me in detail or give me guidance for a good website from where I can learn c++ to prepare a windows application.
I am again thankfull to you and wish u best wishes.

Regards

Nazir Hussain
Jun 5 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Daniel Ng | last post by:
Thanks for the 2 guys how hep to to solve to count the number of recount in forum. I'm done wif that. Got 1 more question.. Example Record 1: ID 2 VALUE 2 Record 2: ID 3 VALUE 3 Record 3: ID 2...
12
by: Curten | last post by:
Hi, I'm using ifstream to read data from a txt-file but the last line is always read twice. How Come? The condition for the loop in which the file is read is while(!infile.eof()) so this...
2
by: Kenneth | last post by:
Ok, so logically this code seems to make sense but for some reason it reads the last record twice before hitting eof. Is ifstream.eof() implementation dependent? void LoadFile(ifstream &File,...
4
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
2
by: ahmedlasheen | last post by:
hello every body when iam trying to delete the last record in DGV this erro appears "Input string was not in a correct format." this error appears only when i am trying to delete the last record....
3
by: rutabuga | last post by:
after opening the text file the the prog is soposed to read each record into certain variables and then display the variable name to the screen the problem i am geting is that the last record in...
2
by: mahmoodn | last post by:
I want to pass a string as file name to a ifstream object, but I can't. char cnt_string = ""; string f = "ss_"; int cnt = 1; string seq = itoa(cnt, cnt_string, 10); string file = f+seq+".txt";...
2
by: Chocolade | last post by:
Thats not the complete code but thats the line givem e the exception: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
1
by: jay manu | last post by:
I have to import an excel file to Arcmap10 using arcpy script. file name,file path etc will be selected from fileDialog(). Can anyone help me, how i can import the selected excel file into...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.