Import text from file, line by line, and put it into a string array [SOLVED]

Started by Akatosh, Mon 22/01/2007 17:26:20

Previous topic - Next topic

Akatosh

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.

Ashen

#1
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, 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:
Code: ags

  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.
I know what you're thinking ... Don't think that.

Akatosh

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++
Code: ags

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.

scotch

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.

Akatosh


SMF spam blocked by CleanTalk