Text file help?

Non-platform specific questions.

Text file help?

Postby SuperSonic » Thu May 19, 2011 10:35 pm

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
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Text file help?

Postby skydereign » Thu May 19, 2011 10:41 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Text file help?

Postby SuperSonic » Thu May 19, 2011 11:26 pm

Thanks this is perfect :D
+point :wink:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest