Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akatosh on Mon 22/01/2007 17:26:20

Title: Import text from file, line by line, and put it into a string array [SOLVED]
Post by: Akatosh on Mon 22/01/2007 17:26:20
So, I've got that text file. And I've got that string array. And now, is there any easy way to get the text file's contents in the string array (line one in array[0], line two in array[1],...)?

If no, is there any way at all? If no, is there a plugin which makes this possible?

Thank you for your time.
Title: Re: Sex! Now I've got your attention: LoadFromFile?
Post by: Ashen on Mon 22/01/2007 17:31:18
Please change the title, it's not really descriptive, is it?

Have you tried anything yourself yet, and if so, what? AFAIK it should be possible using the File functions (http://www.adventuregamestudio.co.uk/manual/File%20functions%20and%20properties.htm), but I'm not quite sure how. (Oddly, I was about to start playing with something similar myself so if someone can beat me to working out the details...)

EDIT:
On a basic level, it looks like something like this would work:

  int temp;
  File *Test = File.Open("Test.txt", eFileRead);
  while (temp < 3) {
    array[temp] = Test.ReadRawLineBack();
    temp ++;
  }
  Test.Close();
  Display("%s, %s, %s", array[0], array[1], array[2]);


To clarify: File.ReadRawLine only reads one line, but calling it again will read the next line, etc. scotch's code (two posts down) is much better as it includes error checking, but the idea is the same.
Title: Re: Sex! Now I've got your attention: Load Text from File?
Post by: Akatosh on Mon 22/01/2007 17:36:40
Okay, I'll change the title. But first admit you fell for it  ;D

Well, I haven't tried anything yet; I just browsed the help thing. I know how to do this in C++, but in AGS scripting language...

C++

TStringList *StrL;
StrL = new TStringList;
StrL->LoadFromFile("a.txt");


The ReadRawLineBack seems only to work with one line (or the whole text file at once?), and the ReadStringBack funciton description strongly advises not to use this on text files.
Title: Re: Sex! Now I've got your attention: Load Text from File?
Post by: scotch on Mon 22/01/2007 17:38:17
You mean something like this?

String lines[100];
File* textFile = File.Open("file.txt", eFileRead);
if(textFile==null) AbortGame("Can't find text file.");
int i=0;
while(!textFile.EOF && i<100) {
Ã,  Ã,  Ã,  Ã, lines = textFile.ReadRawLineBack();
Ã,  Ã,  Ã,  Ã, i++;
}

That's also fairly similar to how you'd do it in C++ without using the Borland specific TStringList class.
Title: Re: Import text from file, line by line, and put it into a string array
Post by: Akatosh on Mon 22/01/2007 17:44:40
Ah... just perfect. Many thanks! (to you too, Ashen)