30
Apr

I've been fiddling with this for a while, but alas, I am not getting the necessary results. The program compiles fine, but the items in the structures are not necessarily going to the correct item or the correct structure. The first account number is correct. What's supposed to be in the first last name is only half of what's supposed to be the second last name. Then, it just gets crazier and crazier. I really am just stuck on this. I'll copy and paste the function in additional information in a moment here. Any help is greatly appreciated (but please realize I'm only in an intro to C class and have a limited vocabulary and understanding when it comes to programming). Thanks.


Answer:
Your code was basically fine. The only real issue seemed to be that you were reading (and printing) one level too many of the array. It's because of the awkward way eof() works. eof() only returns true after you try to read *beyond* the last byte in the file. So when you use eof() in the while loop as you had it, you basically print out one record beyond the end of the file (aka garbage) because you never checked to see that each of your read() statements returned ok.

I changed a couple of minor things and created my own test file and it seems ok. The way I coded the read() loop isn’t perfect but it should work and give you something to improve upon.

The code is at:

http://pastebin.com/m12253d67

If you are having further problems drop a line.

———————————-

Derek -

Who provided your data? Here’s the easy program I used to create my test data set. Does it produce something different than you’re looking at?

http://pastebin.com/d427a85f4

—————————

Yes, you’ve to write field by field because when the struct is constructed it may (and often will) have slack bytes. The compiler pads because various types need to start on valid address boundaries e.g. an int on a 4 byte boundary. If you write out the pad bytes there’s no guarantee the reading program will make sense of them. If you do a sizeof(struct) it might be different than the sum of sizes of the individual fields.


Answer:
CSCI 240, right?

I'm having the same problem. Tried your code, same problem. The program is reading the strings fine, its the ints and doubles it's not reading right. The program is told to read the file using binary input; could this be the problem?

This is the error it gives me when I try your program, That Guy:

An Access Violation (Segmentation Fault) raised in your program.

Again, the strings are read fine, the ints and doubles do not. And the program stops after it tries to cout the credit limit.

______________________________________…

Oh, didn't know that I had to write each member of the struct to the file. I was just writing the entire struct to the file at once.

It works now.

Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList

This entry was posted on Wednesday, April 30th, 2008 at 2:30 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or TrackBack URI from your own site.

Leave a reply

Name (*)
Mail (*)
URI
Comment