GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Aug-2006, 18:44
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road

random access file read problem


Hello all,
Please bear with me. My first post and very new to C.

In a program I have written, I have code that writes data to a random access file created (opened) as follows:

CPP / C++ / C Code:
fPtr = fopen( "f:\\PRG167\\final\\customers.dat", "wb+"

No problems writing records, everything works just fine.

I have a problem when I am reading the data back out. The code is as follows:

CPP / C++ / C Code:
void disp_tot( FILE *fPtr )
{

    int i;

    double w_temp;
    double c_temp;
    double p_temp;

     customer_t customer;
    
    rewind( fPtr );
    
    while( !feof( fPtr) )
    {
        fread( &customer, sizeof( struct customer_s ), 1, fPtr );
        for( i = 0; i  < 5; i++ )
        {
            z_temp = customer.zone[i];

            if( isalpha(z_temp) == 0 )
                break;

            // read data from file and assign to vars.

            z = customer.acct;
            
            w_temp = customer.weight[i];
            c_temp = customer.charges[i];
            p_temp = (double) customer.package[i];

            printf( "cust=%i zone=%c weight=%.2f charges=%.2f packages=%.0                                   f\n", z, z_temp, w_temp, c_temp, p_temp);
         }

         // my weak (and unsuccessful) attempt to break out of the loop         
         
         if( feof( fPtr) )
             break;
     }
        
    /*other unrelated stuff here
    .
    .                           */
    
}

expected output:
cust=1 zone=A weight=10.00 charges=0.61 packages=1
cust=1 zone=B weight=11.00 charges=0.71 packages=1
cust=2 zone=C weight=11.00 charges=0.83 packages=1


actual output:
cust=1 zone=A weight=10.00 charges=0.61 packages=1
cust=1 zone=B weight=11.00 charges=0.71 packages=1
cust=2 zone=C weight=11.00 charges=0.83 packages=1
cust=2 zone=C weight=11.00 charges=0.83 packages=1

As you can see, the last record is being read in twice. Other than that, I'm not having any issues.

I know this situation has been discussed in this forum before, I searched first before posting this. The problem is that I can't relate the solution I saw to what I'm trying to accomplish here.

My thanks in advance for any advice or help that can be offered.

M. McCoy
  #2  
Old 17-Aug-2006, 19:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: random access file read problem


Quote:
Originally Posted by wmmccoy0910
I have a problem when I am reading the data back out. The code is as follows:

CPP / C++ / C Code:
    while( !feof( fPtr) )
    {
        fread( &customer, sizeof( struct customer_s ), 1, fPtr );
         }

         // my weak (and unsuccessful) attempt to break out of the loop         
         
         if( feof( fPtr) )
             break;
     }
  
}

As you can see, the last record is being read in twice. Other than that, I'm not having any issues.


I am amazed at the number of books (and examples from people's classes) that have this kind of error.

The end-of-file flag (tested by the feof() function) is only set after the program tries to read beyond the end of the file.

So, you could use the if(feof()) test after you attempt to read but before you use the data that your program is assuming contains new stuff. (So the while(feof()) is not properly used in your case.)

A better idea, in my opinion is that you should always (always) test the return value of input functions (whether they are fread(), fscanf, or whatever...).
If fewer things were read than you expected then there is some kind of problem with the input. It could be that you have attempted to read past the end of file (you can use feof() here as a further test), or it could be some other kind of problem. In any case, your program should take this into account. Sometimes it is convenient to use the number of items read as the loop control.


You could try something like:

CPP / C++ / C Code:
    
    while(fread( &customer, sizeof( struct customer_s ), 1, fPtr ) == 1)) {

      /* here is the business part of the loop */

}

Regards,

Dave
  #3  
Old 17-Aug-2006, 19:20
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 917
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: random access file read problem


Quote:
Originally Posted by wmmccoy0910
I know this situation has been discussed in this forum before, I searched first before posting this. The problem is that I can't relate the solution I saw to what I'm trying to accomplish here.

Well, if you read this guide, you will see it advises against using feof as a test for exiting a loop. The suggested work-around(s) revolve around the checking done on the return value from the read function; in your case, that is fread. As far as I can remember, it returns 0 or some negative value in case the read operation was unsuccessful. Use that to check whether it is time to exit the loop or not.

HTH,
Lucian

[edit]Dave's response is more insightful, as always [/edit]
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 17-Aug-2006, 20:07
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road

Re: random access file read problem


My sincere thanks to to Dave for his reply. He obviously read my post and understood that I had already read the post that Lucian referred to. As I stated, I read what was out there and could not relate it to my issue.

I try to never post without searching first; I could not make the post Lucien referred to fit my template. I am, after all new to C.

Again, Thanks Dave!
  #5  
Old 17-Aug-2006, 20:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: random access file read problem


Quote:
Originally Posted by wmmccoy0910
As I stated, I read what was out there and could not relate it to my issue.

It's always OK to ask. Sometimes the same information from different points of view just seems right. Sometimes---not so much.

Regards,

Dave
  #6  
Old 17-Aug-2006, 23:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: random access file read problem


Quote:
Originally Posted by wmmccoy0910
My sincere thanks to to Dave for his reply. He obviously read my post and understood that I had already read the post that Lucian referred to. As I stated, I read what was out there and could not relate it to my issue.

I try to never post without searching first; I could not make the post Lucien referred to fit my template. I am, after all new to C.

Again, Thanks Dave!
Simply a curiosity question. If you read the tutorial that said "don't use feof() to exit a loop, and gave
CPP / C++ / C Code:
while (!feof(st))
as an example what not to do, how could this not relate to your loop
CPP / C++ / C Code:
while( !feof( fPtr) )
which is an identical statement? As I said, I'm just curious...
__________________

Age is unimportant -- except in cheese
  #7  
Old 18-Aug-2006, 00:44
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road
Unhappy

Re: random access file read problem


Quote:
Originally Posted by WaltP
Simply a curiosity question. If you read the tutorial that said "don't use feof() to exit a loop, and gave
CPP / C++ / C Code:
while (!feof(st))
as an example what not to do, how could this not relate to your loop
CPP / C++ / C Code:
while( !feof( fPtr) )
which is an identical statement? As I said, I'm just curious...

Just curious. What does it matter if I got the information I needed in a way that I could digest it? I made it very clear that:

1. My code was wrong and I knew it and stated same.
2. I had already read the tutorial and I could not apply (and understand) it because I'm new to C and
3. I stated that as well.

Dave seemed to understand what the gist of my message was and gave an answer that used my existing (bad) code in context, which he provided without prejudice or sarcasm. I was immediately able to integrate it and get on with what I was going.

So, I'm just curious. Was your post meant to be helpful? Was it to make me feel real good and welcome about being a first time poster in this forum? Thought so...

I know you help a lot of folks here, but I don't understand for the life of me what your post adds to this thread. If I hadn't already read the "tutorial", I might understand your subtle sarcasm, once again, for possible penetration, I stated clearly that I had already done a search for my problem, read the tutorial, and still was having a problem. Maybe I'm just plain stupid, but when I joined this group, It didn't say anywhere that I couldn't be so.

Maybe it would have been more appropriate for Dave to give me a decent flame job first, but I'm very grateful that he chose patience over arrogance.

Now, what was your question...?

Just curious.
  #8  
Old 18-Aug-2006, 00:49
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road
Thumbs up

Re: random access file read problem


Quote:
Originally Posted by davekw7x
It's always OK to ask. Sometimes the same information from different points of view just seems right. Sometimes---not so much.

Regards,

Dave

Dude, you're awsome, and it's so refreshing to get an answer from someone who's first mission isn't flame first, answer questions later.

I won't forget it...

Best regards,
M
  #9  
Old 18-Aug-2006, 03:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: random access file read problem


Quote:
Originally Posted by wmmccoy0910
Just curious. What does it matter if I got the information I needed in a way that I could digest it? I made it very clear that:

1. My code was wrong and I knew it and stated same.
2. I had already read the tutorial and I could not apply (and understand) it because I'm new to C and
3. I stated that as well.

Dave seemed to understand what the gist of my message was and gave an answer that used my existing (bad) code in context, which he provided without prejudice or sarcasm. I was immediately able to integrate it and get on with what I was going.

So, I'm just curious. Was your post meant to be helpful? Was it to make me feel real good and welcome about being a first time poster in this forum? Thought so...
Whoa -- calm down, please. I wrote that tutorial so I was curious because I wondered what it was you missed so I could decide if a rewrite would help. I tried to ask in such a way as to not get you up in arms and obviously failed. Sorry. It was a honest request for information, not sarcasm. Sometimes we ask questions for information, not to flame someone.
__________________

Age is unimportant -- except in cheese
  #10  
Old 18-Aug-2006, 13:43
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road
Smile

Re: random access file read problem


Quote:
Originally Posted by WaltP
Whoa -- calm down, please. I wrote that tutorial so I was curious because I wondered what it was you missed so I could decide if a rewrite would help. I tried to ask in such a way as to not get you up in arms and obviously failed. Sorry. It was a honest request for information, not sarcasm. Sometimes we ask questions for information, not to flame someone.

No need to ask me to calm down, I wasn't mad, but your post, did come across as being somewhat sarcastic whether you meant it or not.

Perhaps if you added what you included in the above quote, I would have most definately interpeted it in an entirely different way.

You have to understand, most newbies are shell-shocked by the treament they get for asking questions that seem so silly by experienced programmers, and are tweaked to a certain degree...And I have seen some of the posts that would drive anyone nuts, rife with questions and examples that begs a negative response. This is why I felt the need to respond as I did to your original post. I did ALL my homework first, read EVERYTHING in the forum regarding the problem I was having, then carefully crafted my post based on the guidelines put forward here.

So, I guess I'm saying :

1. The info in the tutorial was too generic to be helpful in my case.
2. The fact that there is a tutorial at all is terrific!
3. I did all I could do up front to explain why I posted in the first place.
4. I am not now, or ever was, upset.
5. Read mt post, read your first one, then put yourself in my shoes.
6. I very much appreciate what your utlimate goal is, I wish you had mentioned it up front since I had no idea you had anything to do with the tutorial. Don't forget, I'm new here.
7. I appreciate the time and effort I have noticed you put in to this forum, and I thank you for that.

Best Regards.

Mike
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
File Read Problem. Size limit? jsmain C++ Forum 19 17-Jul-2006 05:22
File write / read problem Tomb332 C++ Forum 2 12-Jul-2006 01:15
Download files in c for windows operating system oozsakarya C Programming Language 5 20-Jun-2006 03:33
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.