View Single Post
Old 27-08-2010, 07:00 PM   #1
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default (C++) Problem with loading a text file to string array using a function

I'm working on an engine for running various simple text-based adventures based on nodes with a limited range of options for the player to select from. (a working beta with a partially complete example module is attached)

Currently, I'm trying to clean up the code so that I can rewrite the event and link parsers into something more effective, but ran into some problems with a function supposed to load the contents of a text file into an array of strings.

What works:
- The number of lines is correctly counted and stored in the counter the external function uses
- The array is created and the lines are loaded from the file into it
- The array contains the correct contents in the filetostrarray() function

What DOESN'T work:
- load_paragraph() doesn't have access to the strings in the array and crashes the program when trying to display them

In void load_paragraph():
Code:
string parfile=par_file(current_node);
int lines;
string *line;
filetostrarray(parfile, &lines, line);
In filetostrarray
Code:
void filetostrarray(string file, int* lines, string *readlines) {
 *lines=lines_in_file(file);
 if (*lines) { fstream line_in(file.c_str(), ios::in);
	  readlines=new string[*lines];
	  for (int i=0; i<*lines; i++) getline(line_in, readlines[i]);
	  line_in.close();
	  line_in.clear(); } }
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote