Page 1 of 1

reading from a file [HELP]

PostPosted: Thu Mar 29, 2012 8:11 pm
by friedfish
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. :) :) :)

Re: reading from a file [HELP]

PostPosted: Thu Mar 29, 2012 8:45 pm
by skydereign
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

Re: reading from a file [HELP]

PostPosted: Sat Mar 31, 2012 4:49 pm
by friedfish
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.