Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Tutorials on text files


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
4 replies to this topic

#1 Jesper T   Members   -  Reputation: 322

Like
Likes
Like

Posted 22 July 2001 - 06:54 PM

Could anyone gimme a link to tutorials on loading from and writing to plain text files ? Or if there is a better way to save info so that i wont loose it if i quit my prog. Actually, i only have to save numbers.. but saving characters might be useful too if that doesnt complicate things too much. JT

Sponsor:

#2 phueppl1   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 July 2001 - 07:09 PM

Cause I think you''re pretty new, I''d start with text files...


first start opening a file:
FILE * file = fopen("save.txt", "wt");
"wt" stands for write textmode....
now you''ve got a handle to an open file...

I guess you know the printf(); function and it''s syntax...
the same with a f in front of it and a little diffrent syntax:

fprintf(file, "%f %d %s\n", 1.875, 16, raw);

and finally close the file:

fclose (file);

this would result in following .txt file

1.87500 16 raw

(how do I show a newline charakter? :p)

to read the stuff:

FILE * file = fopen("save.txt", "rt");

notice the "rt" instead of the "wt" -> read textmode

float afloat;
int anint;
char astring[50];

fscanf(file, "%f %d %s", &afloat, &anint, astring);

watchout that strings in the file may not have spaces if you''re trying to read it with one %s..

ok, hope that helped,
cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states


#3 Jesper T   Members   -  Reputation: 322

Like
Likes
Like

Posted 22 July 2001 - 11:52 PM

yup, that helped
thanks alot.

btw, check out the project im workin on: http://home.online.no/~7396/delta/

#4 Midnight Coder   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 July 2001 - 02:19 PM

If you''re saving numbers, you''d be far better off with binary files. You can save numbers and any other variables/structs/whatever like this:

  
int SaveFile(char *filename)
{
FILE *file;

//Open file (w stands for write). Return 0 on error

if((file = fopen(filename, "w")) == NULL) return 0;

//Write the integers to the file

fwrite(&SOME_DATA, sizeof(int), 1, file);
fwrite(&some_more_data, sizeof(int), 1, file);

//Close file

fclose(file);
}
int OpenFile(char *filename)
{
FILE *file;

//Open file (r stands for read). Return 0 on error

if((file = fopen(filename, "r")) == NULL) return 0;

//Read integers from file

fread(&some_data, sizeof(int), 1, file);
fread(&some_more_data, sizeof(int), 1, file);

//Close file

fclose(file);
}


That should do it. Saving in binary mode also means it will be harder for your users to manually modify their save files to cheat.

#5 phueppl1   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 July 2001 - 06:28 PM

Jup, he''s right, I just think text files are easier to start with :o)... you can also use nice tricks... like encoding your numbers in some fashion.. that makes things harder too :o)))

cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states





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