File read/write

You must understand the Game Editor concepts, before post here.

Postby makslane » Fri Mar 10, 2006 12:15 pm

1) In the Variables panel, change the type of myArray variable to String
2) Change the readLine function to:

Code: Select all
int readLines(char *fileName, int max)
{
   int i = 0;
    FILE *fp = fopen(fileName, "r");
    if(fp)
    {
        while(fgets(myArray[i], 256, fp)) //fgets used to get the whole line
      {
        i++;
      }
 
      fclose(fp);
    }

  //Number of read lines
   return i;
}


3) Change the action code to:

Code: Select all
int total;

total = readLines("file.txt", 100);
strcpy(debug1.text, myArray[0]);
debug2.textNumber = total;
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Sun Apr 23, 2006 12:00 pm

How could this be modified to return just one string? I'm thinking for some things it would be easier to write a peice of text in notepad, rather than in the script editor. For exmple, loading the txt file with:
There was this stuff happenning.
Someone shot someone else.
Another guy exploded.
And using
Code: Select all
readfile(writing);
strcpy(text, writing);

Would be easier than saying
Code: Select all
strcpy(text, "There was this stuff happening.\nSomeone shot someone else.\nAnother guy exploded.");

Also, it would be better where you want a lot of text, like in credits, or reading a story.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Previous

Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest