Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
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
Old 27-08-2010, 07:16 PM   #2
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

The following worked:
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
Reply


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


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 09:56 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.