Tuesday, July 14, 2009

Simple C++ File I/O Question?

Why exactly does this program not work? I want the output to be 150


999





(Ignore the spaces/line breaks, i just want those two numbers)








I have commented the purpose of each of the four seperate peices of my code. All im trying to do here is put the number 150 into a file, then take it out and display it. that works. then, i want to overwrite 150 with 999. i know that works because i can open the file and look in it, it does indeed say 999.





the trouble starts when i try to copy the 999 and display it...i get the original junk that was in the variable 'number2'





(the code is in details because its too much text)

Simple C++ File I/O Question?
This really does seem perplexing. I am not an expert - the only thing I can think of is that it has something to do with the fact that it only takes 1 byte to represent 150 and it takes 2 bytes to represent 999. How do you know that what you are getting is what is in the variable number2 originally? I would be interested to know if instead of using 999 you used another number, like 127.


No comments:

Post a Comment