• Advertisement

Archived

This topic is now archived and is closed to further replies.

writing data to a file

This topic is 6449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, I have an empty text file. I open the file, try to write some stuff to it, and close it. Like this:
    
	FILE *file;
	file=fopen("Data/gamecfg.txt","rt");
	fputs("This is a test",file);
	fclose(file);
    
any ideas? I can read the data fine with fgets, but the function above won''t write data to the file. Thanks, Joe JoeMont001@aol.com

Share this post


Link to post
Share on other sites
Advertisement
There are some mistakes in this line:

file=fopen("Data/gamecfg.txt","rt");

it should be:

file=fopen("Data\\gamecfg.txt","r+");


Edited by - +AA_970+ on June 26, 2000 2:53:18 PM

Share this post


Link to post
Share on other sites
The "r+" access mode allows both reading and writing, but the file has to exist.

+AA_970+

Share this post


Link to post
Share on other sites
Doesn''t "rt" mean "read only text"? You want to have "r+t" so you can read and write at the same time.


Andrew

Share this post


Link to post
Share on other sites
quote:
Original post by +AA_970+

There are some mistakes in this line:

file=fopen("Data/gamecfg.txt","rt");

it should be:

file=fopen("Data\\gamecfg.txt","r+");


Edited by - +AA_970+ on June 26, 2000 2:53:18 PM


That''s not true, / works too, and for maximum portability you should always use / instead of \ when writing paths. DOS and Windows support both / and \ as directory and drive separators, but Linux and Unix only support /. So using / will save you some time if you port your game to Linux/Unix

- Muzzafarath

Mad House Software
The Field Marshals

Share this post


Link to post
Share on other sites
Ok i just checked the VC++ help file, i didn''t realize "t" was actually an access mode. So acraig is correct, it would be "r+t"

BTW, thanks for the tip Muzzafarath, i never knew that you could use the foward slash in filenames

+AA_970+

Share this post


Link to post
Share on other sites
well I guess that's why I could read the data. I got it to work. here's the final code:
    
FILE *file;
file=fopen("Data/gamecfg.txt","r+t");
fprintf(file,"This is a test");
fclose(file);

Thanks guys,
Joe

JoeMont001@aol.com

Edited by - Julio on June 26, 2000 8:50:38 PM

Share this post


Link to post
Share on other sites

  • Advertisement