![]() |
|
#1
|
||||
|
||||
random access file read problemHello 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:
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:
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
|
|||
|
|||
Re: random access file read problemQuote:
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:
Regards, Dave |
|
#3
|
||||
|
||||
Re: random access file read problemQuote:
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 __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#4
|
||||
|
||||
Re: random access file read problemMy 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
|
|||
|
|||
Re: random access file read problemQuote:
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
|
||||
|
||||
Re: random access file read problemQuote:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
Age is unimportant -- except in cheese |
|
#7
|
||||
|
||||
Re: random access file read problemQuote:
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
|
||||
|
||||
Re: random access file read problemQuote:
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
|
||||
|
||||
Re: random access file read problemQuote:
__________________
Age is unimportant -- except in cheese |
|
#10
|
||||
|
||||
Re: random access file read problemQuote:
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 GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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