Any people out there familiar with C++ I/O operations?

Gooblah

Heh...
Joined
Jun 5, 2007
Messages
4,282
As you may or may not know, I'm in the process of constructing a program that runs through XML files and adds tags to both the file and the Schema. Sadly, my program runs into a fatal runtime error when the user input ends and the actual file operations begin.

Attached is a zipfile with the project in question (I ran it through Dev C++ v7.0.2).

Thanks for any help in advance.
 
While possibly not very helpful reply I'm still going to say that C++ might not be the most fitting tool for such a job. It surely can do it but there would be many easier ways.
 
While possibly not very helpful reply I'm still going to say that C++ might not be the most fitting tool for such a job. It surely can do it but there would be many easier ways.

Agreed. You can do a decent job in C++ using Boost::Regex, but it's a pain in the buttocks. This problem calls for Perl (or Python, if you want to be able to reuse the code later)!
 
Code:
	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);
Your arrays are 500 characters long. The file names are followed by hundreds of nulls so file.open and schema.open won't work

Code:
	schema.seekg(ios::beg, ios::end);
	SchemaLength = file.tellg();
	schema.seekg(ios::end, ios::beg);
Shouldn't this be schema.tellg() ?

Also the point of do...while is to make sure it loops at least once, having an if condition in front of it kind of defeats the point. Just use while instead
 
Thanks for the input guys.

I'm not familiar with either Python or perl, so those can be thrown out the window for now. I've gone through and rewritten/streamlined my code (below, in spoiler), and there is only one problem left.

Spoiler :
Code:
	fstream file(FileName.c_str(), fstream::in), schema(SchemaName.c_str(), fstream::in); //objects used to open file and schema
	if (file.is_open() && schema.is_open())
	{
		//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 (int a = 0; a < FileLength - (PositionName.length()+3); a++)
		{
			if (FileBufferString.compare(a, PositionString.length(), PositionString) > 0)
			{
				file.seekp(a + PositionName.length() + 3);
				file << TagString.c_str();
				file.flush();
			}
		}
		for (int b = 0; b < SchemaLength - (PositionName.length()+29); b++)	
		{
			if (SchemaBufferString.compare(b, SchemaPositionString.length(), SchemaPositionString) > 0)
			{
				schema.seekp(b + PositionName.length() + 29);
				schema << SchemaTagString.c_str();
				schema.flush();
			}
		}
		//close files
		file.close();
		schema.close();
		cout << endl << "Please check the files to ensure the program worked properly." << endl;
	}
	else 
	cout << "Cannot open the files.";
	system ("PAUSE");
	return 0;

The program enters the if loops necessary to write to the files - but doesn't do any physical writing.

Edit: Wow, I'm really learning a lot through the troubleshooting and stuff; turns out my parents were at one point hardcore programmers and took courses at university-levels. They've been a great source of info.
 
:bump: K, so I found why the program wasn't actually writing (I needed to declare 'file' and 'schema' as objects of class ofstream, not fstream. :mad:). However, how can I write to the file in a formatted matter from a string?

For example, if I write to the file the string "!!!/n!!!":
Code:
string WriteThis = "!!!/n!!!";
file << WriteThis;

the file will contain:
Code:
!!!/n!!!

However, if I use this syntax:
Code:
file << "!!!/n!!!";

the file will contain:
Code:
!!!
!!!

Considering I can't be constantly writing to the file due to my need to add space every single time (and I REALLY dont want to use my original method of multiple holding buffers and writing/rewriting the file after EVERY tag addition), is there any way to write a string while maintaining format characters?
 
Are you sure you're not just confusing / and \? \n is used to represent a newline character, /n is just what it seems. If you want a backslash in a literal string, use two backslashes.
 
Back
Top Bottom