Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,031 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,602 people online right now. Registration is fast and FREE... Join Now!




binary file I/O

 
Reply to this topicStart new topic

binary file I/O, need help with reading a struct record

rizwans
2 Feb, 2008 - 02:30 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
Hi

I need help with reading a struct record and output it's position within the file.

The struct looks like this:


i am taking input from a binary file which is successful.
then i am reading in the record which is passed by reference.

The part that i am having trouble with is when i am trying to read the record as a whole and output it's position within the file, i keep getting memory leaks.

an example record would look like:
joe blow
123
2500.00

CODE


struct Record
{
     char name;
     int SIN;
     double income;
};



bool submission_search(Record& r, fstream& bin_infile)
{

  bool good_read;

      if (good_read)
        {
          bin_infile.read(reinterpret_cast<char *>(&r), sizeof(Record));
          streampos submission = bin_infile.tellg();
          cout << endl;
          cout << "submission # " << submission << ':' << ' ' << "name= " << r.name << ','  << ' ' << "SIN= "  << r.SIN << ' ' << "income=$  " << r.income << ' ';
          cout << endl;

        }
      else
        {
          good_read = false;
          cout << "There is no record containing that submission number.\n";
        }
      return good_read;
}



I am confused as to how to properly use the bin_infile.tellg().
I know that it would save the current position but it doesn't seem to hold it.
Also, do i need to check for eof before i attempt a read?

This post has been edited by rizwans: 2 Feb, 2008 - 02:46 PM
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: Binary File I/O
2 Feb, 2008 - 02:41 PM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,215



Thanked: 15 times
Expert In: Hardware, Networking

My Contributions
Please post your code in code tags: code.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month