Page 1 of 2

File read/write

PostPosted: Tue Feb 21, 2006 5:57 am
by Joshua Worth
how do you you use the file read/write functions?

PostPosted: Tue Feb 21, 2006 6:25 pm
by makslane
Do you mean saveVars?

PostPosted: Tue Feb 21, 2006 9:05 pm
by Joshua Worth
No, I mean fopen+fclose etc.

PostPosted: Wed Feb 22, 2006 2:31 pm
by makslane
You can see example in standard C programs.
Look at google for fopen, fwrite, ...

PostPosted: Sat Feb 25, 2006 10:57 am
by Joshua Worth
can you give me a straight foward way to open a text file, read each line, turn them into integers instead of strings, and put each into an array.

PostPosted: Sat Feb 25, 2006 11:07 am
by makslane
What's the file format?
Something like this?

10
12
30
45

PostPosted: Sat Feb 25, 2006 10:09 pm
by Joshua Worth
yes

PostPosted: Mon Feb 27, 2006 9:03 pm
by Joshua Worth
Please give me an answer quickly, i havent got long :shock: :!:

PostPosted: Tue Feb 28, 2006 7:55 pm
by makslane
Put this code in the Global Code editor:

Code: Select all
int readLines(char *fileName, int max, long *array)
{
   int i = 0;
    FILE *fp = fopen(fileName, "r");
    if(fp)
    {
        while(fscanf(fp, "%d", &array[i]) > 0)
      {
        i++;
      }
       
      fclose(fp);
    }

  //Number of read lines
   return i;
}



If you are using a array of integer (with 100 elements), you can use this code in the Script Editor:

Code: Select all
readLines("file.txt", 100, myArray);


Download the sample here:
http://game-editor.com/examples/readlines.zip

PostPosted: Wed Mar 01, 2006 6:44 am
by Joshua Worth
thankyou soooooooooooooooooooooooooooooo much!!!!!!!!! :D :D :D :D :D :D

PostPosted: Wed Mar 08, 2006 6:24 am
by Joshua Worth
How can i make it read into a string instead?

PostPosted: Wed Mar 08, 2006 12:31 pm
by makslane
Create a array of strings and use the code:

fscanf(fp, "%s", array[i]) //Without &

PostPosted: Wed Mar 08, 2006 8:55 pm
by Joshua Worth
I have to change the long *array at the top dont i?

PostPosted: Thu Mar 09, 2006 2:13 am
by makslane
yes

PostPosted: Fri Mar 10, 2006 5:27 am
by Joshua Worth
what to?