I don't have any errors when I compile, so that's a good start. :)
I need to read a text file. Then I need to view, write over data, and add an additional entry (I have it set to 2 now for testing purposes).
Right now, when I read, I just get a bunch of gobbledy gook and my choices seem to loop and don't go back to the menu when one entry has been accessed.
THANK YOU TO ALL THAT READ THIS. (I removed sections to make it a shorter read. If you can show me what I'm doing wrong, I can apply it to the other functions.)
Your menu() function never returns a value. Your compiler should have warned you about that.
Also, the way you've read your data in is completely wrong. You're assuming that the cast will automatically de-serialize your data from string form to structure form, and in reality, it is much more complicated than that. I suggest you read up on the stream extraction operator (operator>>) and getline() and use them to appropriately read in your input.
Edit: Since you already pass in the open filestream into your function, you should just use that reference instead of trying to open the file again. And the same issue goes for outputting your data.
I really appreciate your time, yulingo. However, I don't understand. I'm using a C++ Early Objects book and it doesn't give good examples.
I'm trying to get the 3 examples from the book to work together. Each was originally a main function, that's probably why the opening and closing of the file doesn't make sense.
I am trying to use text files so I can view them to help troubleshoot my problem. I have populated the file with data and left the file blank. Neither fixes the problem.
well you can't use text values with this parsing method. You have to add a few entries first and try reading them back out to see if you can parse what you wrote.
Let me clarify:
the read() function reads in a block of data from a file, literally as a bunch of raw bytes. Therefore, the data in the file has to be in raw byte form, whch is not the same as text in a text file. For example, a double value like 2.3 is actually stored in 8 bytes of computer memory. the reinterpret_cast can't magically transform "2.3" in a text file into a double value.
The way my code is written, will it save my entries, or will it delete them all when I close the program. (The book never says what happens to the entries).
Should I change the file I'm writing to into a .dat file?
Thank you for all of your time and efforts!
How do I stop the loop once I enter an entry and go back to the menu selection? I can't test if it stores anything because it keeps going to "Enter the record number of the item you want to edit:"
well it really doesn't matter which extension you use, because no matter what, you can still try to open the file in a text editor and it won't look like a normal list of data because it is all in raw bytes.
That is the biggest drawback to these examples-you can't actually see if something is written wrong unless you read it back. That's why so many assignments are about text parsing.