I can use the program fine without this code but I need to be able to take a line of text a file and read it into a variable. Here is the code causing the app crash:

string ReadLine()
{
	getline(file, lineread);
	if (lineread.find_first_of("WSF_") != string::npos)
	{
		return lineread;
	}
}

What I need it to do is take a line in the text file and if it is not a W S F or _ it is an invalid line and my program will default to a hardcoded value for my maze.

Row1 = ReadLine();

Row1 is a string and the readline should set Row1 to lineread.

The first line currently contains: WWWWWWWW

Not using the function lets the program run but otherwise it crashes. Is there any way to get a line at a time from the already loaded file and have it returned as a string like it should be to the program?

I've done this before. do you want it to take a specific line or just have it return the first and only?


This is the first version of my function. i have another that can return the line only if it starts with a specific substring

string getfile(string file, int line)	

{
int j = 1;
string retdata;
string notfound = "line not found";

ifstream data( file.c_str() , ios::in);

while ( ! data.eof())
{
getline(data,retdata);
	if (j == line )
	{
	data.close();
	return retdata;
	} 
j++;
}
data.close();
return notfound;	
}

or are you looking for a bool that just returns true if it is there?

commented: This was the right was I have to make a change to my file.open() line but this helped me solve it. +1

That looks good it just has to return one line of the file at a time. The program will have a file that has 9 total lines the maze consisting of the first 8 of the lines.

So the Maze.txt would look like this:
Ignore the Poor formatting do to the non uniform spaced font.

WWWWWWWW
W_W__W_W
W_WW_W_W
W__W_W_W
WW_W___W
W__WWW_W
WW_____W
WWWWWWWW

BGM C:\\BGM.wav

The above is just an example but it would take from the file one line at a time and I will likely pass a CurrentLine variable to the function it will increment.
If you can help me get it to return a single line that would help alot.

Where is "file" coming from in your listing in the OP?

pass it a pointer if you want to change values across the function call. can you write out the function as you will be using it? i.e

int x = randomint(int highest, int lowest)

if you want to return every line you can just that function i posted and nest it inside a for loop. increment the variable "line" each loop and if the function returns "not found" have it break.

It is preloaded in this function:

int OpenFile(HWND hWnd, string FileName)
{
	file.open(FileName.c_str());
	if (!file){
		MessageBox(hWnd, "Error in opening file", "Scripting: Error", MB_OK);
		return EXIT_FAILURE;
	}
}

Rather than have a global ifstream object, declare it in main() and pass it around by reference.

In your ReadLine function, what happens if your line doesn't contain any of those characters? If the function is not void, it's good practice to have a return for all possible paths through the function.

None of these problems should lead to a crash, though. It is more than likely something else. How did you determine that the function you posted was crashing the program?

pass it a pointer if you want to change values across the function call. can you write out the function as you will be using it? i.e

int x = randomint(int highest, int lowest)

if you want to return every line you can just that function i posted and nest it inside a for loop. increment the variable "line" each loop and if the function returns "not found" have it break.

I know how I can do it but I just need an example of a function that will using my file in the function give me a single line and return it by the return statement so I can give it for example:
ReadLine(int Line)

where ReadLine for Maze.txt I showed you above yields:
ReadLine(1) == "WWWWWWWW"

...Here is the code causing the app crash:

string ReadLine()
{
	getline(file, lineread);
	if (lineread.find_first_of("WSF_") != string::npos)
	{
		return lineread;
	}
}

The function is basically badly broken -- what will it 'return' if lineread.find_first_of("WSF_") == string::npos ?

Aren't you getting any compiler warnings?

ok, i think i understand you, you want it to just return the first line? the function getfile i showed you will do that. have a look through the code and see what it does. if you use it like so:

string data = getfile("Maze.txt", 1);

data will be equal to "WWWWWWWW". if you want the function to be used only for Maze.txt, just edit it accordingly.

Ok thanks lexusdominus I have got it reading the lines from the text file now.

I just have to fix a few things and that part of the program will be complete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.