Click to See Complete Forum and Search --> : sstream, fstream & debugging


silvrwood
October 26th, 2004, 04:49 AM
I have a code that uses 2 AVLtree derived type specific trees and performs various procedures on them. In one of the procedures it searches the tree type's vector for an occurence of a string. Generally the data structures look like this:

tree<type>

class type : public typeI
{
private:

int oneInt;
int twoInt;
vector<string> vec;
};

class typeI
{
private:

string one;
string two;
};

Anyway, when I run the program in debug mode, everything runs smoothly. When I run it without debug mode, and the string is not found in the vector, I get this:
<<
Unhandled exception at 0x00429176 in Lab4.exe: 0xC0000005: Access violation writing location 0x0009000d.

This occurs at
static void __cdecl assign(_Elem& _Left, const _Elem& _Right)

{ // assign an element

_Left = _Right;

}

on the line _Left = _Right;

>>
Why would this occur, and specifically only when I'm not in debug mode?

Also, I use a set method to set the data for for the derived class type. When I simply create strings then pass them to a set method, all runs well. When I use an istringstream object to parse the strings in a line from a file, the base class members aren't set. Any suggestions there?

<<
string line;
string first;
string last;
int num;

type newType;

while ( !infile.eof() ) //infile was passed as a ref to this function
{
getline( infile, line );
istringstream sstream( line );
sstream >> first >> last >> num;
newType.setType( first, last, num, 0 );
typeList.insert( newType ); //this is the object of tree<type>
// it was passed to the function as a reference
}

infile.close();
>>

And finally, when I try to open a hardcoded file then call a getFileName function if the hardcoded file does not open, the file cannot open. If I just call the getFileName function without trying to open a hardcoded one, it opens fine. Any suggestions there?

ifstream file2( "dat.txt" );

if ( !file2 )
{
cout << "Cannot open " << file2 << endl << endl;
getFileName ( file2 );
}

//The definition of getFileName
void getFileName (ifstream &instr )
{
bool end = false;
string name;

while ( !end )
{
cout << "Enter a file name --> " << flush;
cin >> name;

instr.open( name.c_str() );

if ( !instr )
{
cout << "Cannot open " << name << endl;
instr.clear();
}
else
end = true;
}
}