Page 1 of 1

Text file help?

PostPosted: Thu May 19, 2011 10:35 pm
by SuperSonic
So I have a text file that looks like this...

MainMenu
Level1

and I would like to change it using the script editor to something like this...

Level1
Level2

Can any of you guys tell me how to do this? :P

Re: Text file help?

PostPosted: Thu May 19, 2011 10:41 pm
by skydereign
First off, to open a text file, you have to use a FILE*, and the function fopen.
Code: Select all
FILE* file = fopen("fileName", "r");


The "r" means you will just read the file, but in your case it sounds like you might want to completely overwrite it. To do that use "w" instead. But, if you want to update a file, you can use "r+". There are several more options, but those should cover most of the things you would do.

Next you have to write the actual text into the file, so you can use this.
Code: Select all
FILE* file = fopen("fileName", "w");
fprintf(file, "Level1\nLevel2\n");
fclose(file);


That will print what you want into the text file, but of course it isn't dynamic. If you want to write more than that, I'd have to know how you are creating the information that you want to write into the file. But, when you are done editing the file, you need to close it, like in the above code. The format for fprintf is the same as for sprintf if you know how to use that.

Re: Text file help?

PostPosted: Thu May 19, 2011 11:26 pm
by SuperSonic
Thanks this is perfect :D
+point :wink: