i would never ask for debugging help usually but i am totally lost and have wasted far too much time on this. both peices of code compile correctly. the original function works fine with my program and the second one crashes.

the original code checks a textfile in the root directory of the program. in the textfile, it then checks each line for a substring and returns true if it finds the substring.

bool determinefile(string full, string filename)

{
     string file;
     file = filename;
     ifstream data(file.c_str(), ios::in);
     string temp;
     while (getline(data, temp))
     {
           if (determine(temp, full))
           {
           data.close();
           return true;
           }
     }
     data.close();
     return false;
}
*

i wanted to change the function to return true, only if the substring is at the start of the line. it appears to work, but my program crashes and i dont know why. it doesnt crash when the function is called, or when it returns, but when the loop it is called from ends. my debugger tells me

First-chance exception at 0x7c812afb in test3.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012d054..
Unhandled exception at 0x7c812afb in test3.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012d054..
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

here is the code :

bool determinefile(string full, string filename)

{
     string file = filename;
	 string infilename;
     ifstream data(file.c_str(), ios::in);
     string temp;
	 int size = full.size();
	 int checker = 0;
     while (getline(data, temp))
     {
		 for(int counter = 0; counter < size ; counter++)
		 {
		 infilename = infilename + temp.at(counter);
		 }
	if (infilename == full)
		 {
			 data.close();
			 return true;
		 }
     infilename = "";
     }
     data.close();
     return false;
}

Im not passing any pointers or references to my function, and i cannot see how it could change any aspect of the function from which it was called. all it does is return true or false. any help would be much appreciated!! thanks

Could you explain what the second function does.

You are using the size of full to test characters in temp. Are you sure temp has >= characters than full? Display the values and see.

the second function does the same but only returns true if the substring its checking for is at the start of the string "temp" taken from the file by getline(). i forgot to say that in the first version of determine2() i use a function called determine(), which takes a larger string and checks it for a substring, returning true if it finds it anywhere.

yeah temp is always larger than full, though thanks for noting that because full is taken at runtime so its a potential bug.

it appears to work, though, it returns true, then the program crashes when it exits the loop its nested inside.

ok, more information. when the program crashes im pointed to this in xthrow.cpp:

_CRTIMP2_PURE __declspec(noreturn) void __CLRCALL_PURE_OR_CDECL _Xout_of_range(_In_z_ const char * _Message)
	{ // report an out_of_range error
	_THROW_NCEE(out_of_range, _Message);
	}

I repeat:

You are using the size of full to test characters in temp. Are you sure temp has >= characters than full? Display the values and see.

commented: a minimal amount of information solving a massive problem +2

thanks waltp, you solved my problem. i had a file where temp was less than full. i wouldnt expect an answer as helpful as that anywhere but daniweb.

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.