Jump to content

  • Log In with Google      Sign In   
  • Create Account


File manipulation problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 06 March 2012 - 11:23 AM

Hello once again!
I'm having some problems with a file stream. I Would be glad if somebody would help me Posted Image

void SaveCharacter()
{
freopen("SaveGame.txt","w",stdout);
printf("%s\n",PlayerName);
printf("%d",PlayerExp);
//fclose(stdout);
}
void LoadCharacter()
{
freopen("SaveGame.txt","r",stdin);
scanf("%s\n",PlayerName);
scanf("%d",&PlayerExp);
//fclose(stdin);

I'm trying to use these 2 voids but it gets stuck when not using fclose and it gives a "handle is invalid" error when i use fclose. What could be the problem? I will probably put all the code in pastebin and put the link later. Link (the voids are at the end. They are being used in the MainMenu() void.).

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7928

Like
1Likes
Like

Posted 06 March 2012 - 11:42 AM

I'd guess that what you experience as being stuck is just that your program prints everything to the file instead of to the screen after the you have redirected the standard output to the file instead, and likewise trying to read any input from the saved file instead of the keyboard. And I would also guess that you cannot close the standard input and output handles, which is why it says the handle is invalid.

But that aside, why are you saving and loading via the standard input and output, and not just through a temporary file handle?

#3 Tribad   Members   -  Reputation: 835

Like
0Likes
Like

Posted 06 March 2012 - 02:01 PM

freopen closes the stream that gets replaced. stdin/stdout are no longer available.

#4 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 06 March 2012 - 02:11 PM

Brother Bob - Can you give me an example code? I actually wanted to use files for this small game. To save & load characters. What other way could I use?

#5 Brother Bob   Moderators   -  Reputation: 7928

Like
0Likes
Like

Posted 06 March 2012 - 02:18 PM

Open the file with fopen, and then use fprintf and fscanf to write and read.

#6 Kheyas   Members   -  Reputation: 179

Like
0Likes
Like

Posted 06 March 2012 - 02:40 PM

Thanks! I got it finally.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS