reading from a file [HELP]

Non-platform specific questions.

reading from a file [HELP]

Postby friedfish » Thu Mar 29, 2012 8:11 pm

does anyone know how to read from a file on gE?

for example:
read(file.txt); //read it line by line
var1 = line1;
var2 = line2;

you read the data from a file then set it to any variable you have after reading it.
any help would be great. trying to figure it out myself as well. thank you. :) :) :)
User avatar
friedfish
 
Posts: 75
Joined: Wed Mar 14, 2012 2:31 pm
Location: UK
Score: 7 Give a positive score

Re: reading from a file [HELP]

Postby skydereign » Thu Mar 29, 2012 8:45 pm

You need to use a FILE*. All it is is a pointer to a file, which allows you to use the file io functions.
Code: Select all
FILE* file = fopen("filename", "r");
// this creates a FILE* variable called file
// the fopen opens a file filename, with certain permissions
// in this case "r" means read permission
// to write you can use "w" (beware opening a file like this overwrites the file


Now when you have a valid FILE*, you can use fscanf and fgets to extract strings from the file, and fprintf and fputs to write into a file.
Code: Select all
FILE* file_p = fopen("filename", "r");
fscanf(file_p, "%s", string1);
fscanf(file_p, "%s", string2);

fclose(file_p); // lastly is to use fclose to actually close the file
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: reading from a file [HELP]

Postby friedfish » Sat Mar 31, 2012 4:49 pm

oh! Thanks for this sky! yes I was looking for the File pointer and file opener. I know how to use it on c++ but idk how to do it on gameEditor.
User avatar
friedfish
 
Posts: 75
Joined: Wed Mar 14, 2012 2:31 pm
Location: UK
Score: 7 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest