writing data to a file

Started by
6 comments, last by Julio 23 years, 9 months ago
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
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
It''s because you are opening the file for read access only.

Mark Fassett
Laughing Dragon Entertainment
http://www.laughing-dragon.com

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

The "r+" access mode allows both reading and writing, but the file has to exist.

+AA_970+
Doesn''t "rt" mean "read only text"? You want to have "r+t" so you can read and write at the same time.


Andrew

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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
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+
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

This topic is closed to new replies.

Advertisement