|
binary fstram object |
aadfo824
Member #7,265
May 2006
|
I'm trying to get a binay file open to save the high scores of the game I'm writing. Heres how I'm doing it. (posted in order of calling) [global] fstream sav; [main()] sav.open("hssav.sav", ios::binary|ios::in|ios::out); [game() function called by main()] textprintf(buffer, font, SCREENW / 2, 30, BLUE, "Opened? %d", ((int)sav.is_open())); //prints the integer result of (int)sav.is_open() [exit(function called by main)] sav.close(); (int)sav.is_open is returning 0, I think that means that the object couldn't open the file. Am I right? Assuming I am, why would that be? Doesn't open(...) create the file if it doesn't exist. If not, how do I go about creating the file? That is is important because I want hssav.sav to be created at runtime if it doesn't already exist. I haven't tried reading or writing yet, I figure there isn't a point in trying to read or write if there is no file. |
ReyBrujo
Moderator
January 2001
|
Have you tried passing the full path instead of just the file name? Like C:/test/hssav.sav (note that you use /, not the evil \). Does it work with the full path? Try not to use spaces in the path. -- |
ImLeftFooted
Member #3,935
October 2003
|
Maybe you don't have permissions to write to the working directory? Maybe your OS cant handle having input and output on a file at the same time? |
aadfo824
Member #7,265
May 2006
|
1) If I use a full path, I won't be able to move the code around by email or flashdrive, something I puit high value in. Why "/" and not "\", the way Explorer does it? 2) My OS is an updated, genuine version of Windows XP. I think it can handle opening files. The people on msdn forums said that ios::in means that the file has to exist already. That makes sense, so now we have the problem, anyone have a solution? What I want to do is this: 1) Look for "hssav.sav" and read the highscores for display, If it doesn't exist, create it with 0s for high scores. 2) Display the scores throughout the game. 3) When the player dies (and they will, ha ha), compare their scores. If the player did better than the last high score, write his score to "hssav.sav". If I can only open the fstream object in ios::out and ios::binary, will I be able to read from it? Maybe I should create more than one object? |
HoHo
Member #4,534
April 2004
|
Why not try to open the file for reading, if it fails create the file and proceed with opening with ios:in? __________ |
ReyBrujo
Moderator
January 2001
|
Quote: 1) If I use a full path, I won't be able to move the code around by email or flashdrive, something I puit high value in. Why "/" and not "\", the way Explorer does it? That was just to check if you could open the file. That way you would have known if you could open the file or not. Also, in the C language, you use / to separate paths. -- |
Elverion
Member #6,239
September 2005
|
Quote: My OS is an updated, genuine version of Windows XP. I think it can handle opening files. He didn't mean that the operating system was unable to open files. He meant he wasn't sure it could handle opening a file for reading and writting in the way you did it at the same time. But, in this case, yes, it can. When opening a binary file, you open with ios::binary | ios::in | ios::out as to not overwrite data, and to allow you to read and write at the same time. It is kind of like appending, only you still read/write from the beginning of the file. The \ character is just a pain in the ass to deal with. Use / because A) it works, and B) it's not a pain in the ass. If you want to use the full path without having to set it constant within code (such as in a #define statement), then you can get the path from argv[0].
-- |
aadfo824
Member #7,265
May 2006
|
Thanks, now its opening just fine. However, I think I'm writing or reading incorrectly. Here is my code (this takes place in the middle of a project, this isn't all of the code):
there it is, score and level print some huge number like -33686019, and I'm sure that the actual games score and level are correct. does it help if I tell you that opening hssav.sav in notepad leavs me with a blank file? |
CGamesPlay
Member #2,559
July 2002
|
Well, a reinsterpret_cast to a char*? Don't you want a static_cast? -- Ryan Patterson - <http://cgamesplay.com/> |
aadfo824
Member #7,265
May 2006
|
No, anything I've ever read says reinterpret_cast. Besides, switching 6 r casts to s casts gave me 6 compiler errors that weren't there before. I think static cast is only for declared vaiables. |
CGamesPlay
Member #2,559
July 2002
|
Okay, you're right. But you do call seekp (seek the put pointer) and then use the get pointer for access. Try seekg instead -- Ryan Patterson - <http://cgamesplay.com/> |
aadfo824
Member #7,265
May 2006
|
Sorry I haven't posted for so long. In Orlanda (not near my computer in Miami) attending the Florida FFA state convention. I changed the seek thingy to seekg before I read from the file and it still doesn't work. Which seekps should be seekgs? |
CGamesPlay
Member #2,559
July 2002
|
hmm, the only thing I can think of is maybe opening the file in append mode instead of truncate: Quote: In Orlanda (not near my computer in Miami) attending the Florida FFA state convention. Orlando, and did you mean FAA? -- Ryan Patterson - <http://cgamesplay.com/> |
aadfo824
Member #7,265
May 2006
|
Didn't work. Maybe it helps if I tell you that the following code is repeated once per time the player plays. Basically, I put all the code that needs to be repeated every time the player dies into a function bool game(). it returns true if it needs to be run again. Main now looks like this //includes int main{ //initialize allegro, load images, open file or create bool again; do{ again = game(); }while(game); // close down } Works like a charm. the following code resides in game and not main.
And I did mean FFA(Future Farmers of America). Its an agricultural youth organization that I'm involved in for school. I went to recieve an award for excellent poultry judging and to have fun. |
CGamesPlay
Member #2,559
July 2002
|
Quote:
sav.seekp((difficulty-1)*recsize); //difficulty is 1, 2, or 3 int(I want different highscores per difficulty) sav.read(reinterpret_cast<char*>(&hs_level), sizeof(int)); sav.read(reinterpret_cast<char*>(&hs_score), sizeof(int));
You are still using seekp, which seeks what you write to, not where you read from. Quote: I went to recieve an award for excellent poultry judging and to have fun. You were judged on your ability to judge poultry? -- Ryan Patterson - <http://cgamesplay.com/> |
Tobias Dammers
Member #2,604
August 2002
|
Quote: Also, in the C language, you use / to separate paths. Not the language. The language itself doesn't even know such things as files exist. Quite a lot of libraries (including stl and libc) use '/' though. The win32 api, however, doesn't. (Why one would use that for opening files is a different story though). --- |
aadfo824
Member #7,265
May 2006
|
Sorry I should have edited the post. After you said "try seekg instead", I did change it. |
|