|
|
Thread Tools | Display Modes |
27-08-2010, 07:00 PM | #1 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
(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); 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(); } } |
||
|
|
27-08-2010, 07:16 PM | #2 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
The following worked:
In void load_paragraph(): Code:
string parfile=par_file(current_node); int lines; string *line; filetostrarray(parfile, &lines, &line); 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(); } } |
||
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
A Problem With Corrupted File | kamiko | Tech Corner | 8 | 01-06-2007 08:34 PM |
Wizardy Gold On Xp Text Problem.... | Todd | Troubleshooting | 0 | 28-09-2006 12:55 PM |
Windows Loading Problem | Danny252 | Tech Corner | 6 | 07-12-2005 04:48 PM |
File I/o Problem In C/c++ | Kon-Tiki | Programming | 5 | 28-04-2005 06:07 PM |
Weird Function Key Problem | crs estmn | Troubleshooting | 5 | 14-01-2005 11:54 AM |
|
|
||
  |