Thursday, July 30, 2009

C++ function that builds array of structures and returns number of structures?

I've been fiddling with this for awhile, 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.

C++ function that builds array of structures and returns number of structures?
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 is not 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 is the simple program I used to create my test data set. Does it produce something different than you are looking at?





http://pastebin.com/d427a85f4


---------------------------





Yes, you have 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 is no guarantee the reading program will make sense of them. If you do a sizeof(struct) it may be different than the sum of sizes of the individual fields.
Reply: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.

pansy

No comments:

Post a Comment