I/O Operations running into fatal runtime error

Okay, so I'm attempting to write a rudimentary program to add new tags to XML files and their respective Schemas. I'm using Dev C++ v7/0.2 to compile and set up my project, but after all the user inputs are complete, the program runs into a fatal runtime error and dies. Any idea what could be causing this?

The following chunk of code represents the stuff I'm uncomfortable with. I think this may be where the error is coming from.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fstream file, schema; //objects used to open file and schema
	
	char FileNameArray[500];
	char SchemaNameArray[500];
    for (int q = 0; q < FileName.length(); q++)
    {
        FileNameArray[q] = FileName[q];
    }
    for (int w = 0; w < FileName.length(); w++)
    {
        SchemaNameArray[w] = SchemaName[w];
    }
	file.open(FileNameArray, fstream::in);
	schema.open(SchemaNameArray, fstream::in);
	//reads all files
	int FileLength, SchemaLength; //store length of file and schema
	//buffer arrays that hold all characters in the file or schema
	char* FileBuffer;
	char* SchemaBuffer;
	//finds length of file
	file.seekg(ios::beg, ios::end);
	FileLength = file.tellg();
	file.seekg(ios::end, ios::beg);
	//find length of schema
	schema.seekg(ios::beg, ios::end);
	SchemaLength = file.tellg();
	schema.seekg(ios::end, ios::beg);
	//creates arrays for file to be read into
	FileBuffer = new char [FileLength];
	SchemaBuffer = new char [SchemaLength];
	file.read(FileBuffer, FileLength);
	schema.read(SchemaBuffer, SchemaLength);
	string FileBufferString = FileBuffer;
	string SchemaBufferString = SchemaBuffer;

For a start, you don't null terminate FileNameArray and SchemaNameArray.

You don't notice they contain junk, because you don't check for failure to open the files.
Already taken care of with a function call earlier in the function.
1
2
3
4
5
6
7
8
for (int q = 0; q < FileName.length(); q++)
    {
        FileNameArray[q] = FileName[q];
    }
    for (int w = 0; w < FileName.length(); w++)
    {
        SchemaNameArray[w] = SchemaName[w];
    }


Wtf is this? Just call c_str() and be done with it...(assuming you are using std::strings)
It's not taken care of. Have you tried to print one to see what it contains?

Take fierdraco's advice and use FileName.c_str() directly.
Wtf is this? Just call c_str() and be done with it...(assuming you are using std::strings)


Hmmm. Didn't know this function existed - to be honest, a lot of my experience with C++ comes with a semester-long class and a crappy teacher who was replaced with a teacher who had to clean up the damage. Most of my string experience is limited to c strings.

Thanks for the heads up, firedraco and kbw. I'll check it again and post my results (or edit this one).

Edit: Replaced the chunk of code highlighted in firedraco's post with this:
1
2
3
4
5
char *FileNameArray, *SchemaNameArray;
	FileNameArray = new char [FileName.length() + 1];
	SchemaNameArray = new char [SchemaName.length() + 1];
	strcpy (FileNameArray, FileName.c_str());
	strcpy (SchemaNameArray, SchemaName.c_str());


I'm still running into a fatal runtime error...

Edit 2: I inserted an 'is_open' check before all the operations commence; however, the program dies in between the final user input/program output and the is_open check, indicating that there's something wrong either in the string construction (i add strings together to create the tag) or still something weird in the part I thought I fixed with firedraco's and kbw's help.

Edit 3: (Jeez, so many updates...I should have condensed this, sorry.) The construction of strings has no problems - I used an output to check that; neither does the usage of c_str(). I edited out the physical tag addition with the /**/ system and compiled/ran; the program can open, read, and close without any problems. This leads me to the conclusion that my system for adding tags is jacked up. Argh, time to fix it.
Last edited on
Well, given all that's happened in the past (*checks watch*)...erm 1 hour...I thought I'd post a complete update.

I've done as you guys advised and inserted output checks and the is_open check into the program. The program can open, read, and close the files without any problems....however, it's the writing that's posing the problem. This is the code I'm using to add the new tags, with the variables I'm using defined below the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int WOOT = PositionName.length();
	    /*char *TagNameArray, *SchemaTagNameArray;
	    TagNameArray = new char [TagString.length() + 1];
	    SchemaTagNameArray = new char [SchemaTagString.length() + 1];
	    strcpy (TagNameArray, TagString.c_str());
	    strcpy (SchemaTagNameArray, SchemaTagString.c_str());*/
		for (int a = 0; a < FileLength - (WOOT+3); a++)
		{
			if (FileBufferString.compare(a, WOOT + 3, PositionString) == 0)
			{
				file.seekp(a + WOOT + 3);
				file << TagNameArray;
				//file.write (TagNameArray, strlen(TagNameArray));
			}
		}
		for (int b = 0; b < SchemaLength - (WOOT+29); b++)
		{
			if (SchemaBufferString.compare(b, WOOT + 29, SchemaPositionString) == 0)
			{
				schema.seekp(b + WOOT + 29);
				schema << TagNameArray;
				//file.write (SchemaTagNameArray, strlen(SchemaTagNameArray));
			}
		}

The commented out parts are part of a small test I was running and I haven't yet deleted.

FileLength and SchemaLength are int values of the length of the file and schema buffers respectively. Nothing too fancy. FileBufferString and SchemaBufferString are two strings, and are the equivalent of the two buffers (tested with an output). PositionString and SchemaPositionString are the two strings the program constructs to parallel what tag is above the potential position of the user's tag in the file (if that made any sense).
You need to replace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
fstream file, schema; //objects used to open file and schema
	
	char FileNameArray[500];
	char SchemaNameArray[500];
    for (int q = 0; q < FileName.length(); q++)
    {
        FileNameArray[q] = FileName[q];
    }
    for (int w = 0; w < FileName.length(); w++)
    {
        SchemaNameArray[w] = SchemaName[w];
    }
	file.open(FileNameArray, fstream::in);
	schema.open(SchemaNameArray, fstream::in);


with:
1
2
std::ifstream file(FileName.c_str());
std::ifstream schema(FileName.c_str());
Even with your advised changes, the program still isn't writing. Here's the code which is doing the actual writing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		for (int a = 0; a < FileLength - (WOOT+3); a++)
		{
			if (FileBufferString.compare(a, PositionString.length(), PositionString) > 0)
			{
				file.seekp(a + WOOT + 3);
				file << TagString.c_str();
				file.flush();
			}
		}
		for (int b = 0; b < SchemaLength - (WOOT+29); b++)
		{
			if (SchemaBufferString.compare(b, SchemaPositionString.length(), SchemaPositionString) > 0)
			{
				schema.seekp(b + WOOT + 29);
				schema << SchemaTagString.c_str();
				schema.flush();
			}
		}

It's definitely entering the if statements (I used an output test to determine that), but the problem occurs when I hit the '<<' operator AFAIK.
Topic archived. No new replies allowed.