Advertisement

Need small help, not much

Started by March 03, 2000 06:29 PM
5 comments, last by blacksyde 24 years, 6 months ago
OK, I am using a combination of C and C++ so I am not sure which this falls under but I think that it is C. I am also a beginning programmer so this is probably pretty easy for most people. I am trying to write a string to a file with: fp = fopen("saved_game.txt", "r+"); fprintf(fp, "%s", name); fclose(fp); All that is showing up in they file is a blank link (a return). There are characters in the char name, I have printed them out on the screen and yet in the file it is not writing it. Please help
With "r+", the file must exist before the write can occur. Since it sounds like it does, why don''t you try "w" first (just to see if you can write to begin with)? Keep in mind that this will destroy the file contents (so if you have anything in the file beforehand, make sure to back it up).

Is there a reason that you''re using R+?

-Chris



---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
Advertisement
yes, the file is there.
The reason why I am using R+ is because I am reading and writing at the same time. I have set it up so that it will write over the spot that I want it to rewrite. I know that I can write to the file because I have had success with writing ints to the file but if I try to write a char then it will not write anything.
Make sure that fp is a FILE *fp;
Also, I can''t remember but I don''t think that r+ is an option, but w+ and a+ i know are. If you want to read and write, use w+. Try that, and if it doesn''t work let me know. Hope that helps - Cloxs.
I verified in the VC++ v 5 help that "R+" is an option (I did this before responding the first time just to be sure). W+ actually erases the file upon open (but allows read-write access to the blank file), so R+ sounds like the option he wants.

I tried your same source, and everything seemed to work fine(I even tried NOT terminating the string, but it still worked).

Here''s my code (all in main() for a console app with stdio.h included -- saved_game.txt has been created):

FILE* fp;
char testname[5] = "Bill";
fp = fopen("saved_game.txt", "r+");
fprintf(fp, "%s", testname);
fclose(fp);

Unfortunately, that doesn''t help me figure out what''s wrong with yours.

Sorry.

-Chris


---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
Thanks for your help, I figured it out. I was reading in one of my ''teach yourself C'' books and I think it said that fprintf is for ints and floats and to write a char you need to use fputs. I did this and it is working thanks
Advertisement
I just love when these things work on one system and not another -- it just gives me a warm fuzzy (and the code I''ve seen from your site looks OK, too -- figure that one out

Glad to hear you got it working.

-Chris

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com

This topic is closed to new replies.

Advertisement