• Advertisement

Archived

This topic is now archived and is closed to further replies.

stack problem

This topic is 5307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Overview:: I'm working on a project to convert one XML format to a second XML format. Cept, the first is really bad formated XML=/ I'm forced to replace random char's in the XML file and resave it so I can parse it with a XML lib(libxml2). So far, I've only found the use of degree signs and ampersands. Problem:: For whatever reason after the class object File is destroyed I get a stack error around the var "outcome". Now, I thought it was linked to maybe a second problem; but im not sure. Also, for whatever reason, when im reading in a XML file on the 5th loop when i setstr(outcome,NULL) it also sets buffer NULL. It only happens on the 5th loop, but im sure that if I used a second XML file it would change loc in the loop. I stepped though the function many times, and none of the other buffers get set to NULL out of the blue like that. Because of it, it writes a blank "" line when it was a Element in the XML doc. Code::
include "stdafx.h"
#include "File.h"
#include "io.h"

class File {
public:

	
	File(char *docname);
	~File();
	
	void replace_char(char c,char* filler);
	//int replace_string(char*string, char* filler);


private:
	char** lines;
	int numLines;
	char* docname;
	FILE *fin,*fout;
};
File::File(char *docname) {
	fin = NULL;
	if(access(docname,00)) {
		printf("Incorrect file name, please re-run convet.exe\n");
	} else {
	printf("Loading %s...\n",docname);
	File::docname = docname;
		fin=fopen(docname,"r");
		if(!fin) 
			printf("Load failed on %s\n",docname);
		fout=fopen("TMP.xml","w");
		if(!fout)
			printf("Create TEMP file failed.\n");

	}
}

File::~File() {
	printf("Closing...\n");
	//fclose(fin);

	//fclose(fout);

	fin = NULL;
	fout = NULL;
}

void File::replace_char(char c,char * filler) {
	char buffer[1500];
	//buffer = NULL;


	char output[1500];
	//strset(output,NULL);

	char *backbuffer;
	int j=0;
	do {
		
		if(fgets(buffer,1499,fin) == NULL)
			printf("fgets error\n");


			for(int i=0;i <= (int)strlen(buffer); i++) {	
				
					if((c == '\&') && (buffer[i] == c) && (buffer[i+4] != ';')) {
						printf("ampersand found\n");
						strset(output,NULL);
						strncpy(output,buffer,i);
							
						strncpy( output+i,filler,strlen(filler)); 
						backbuffer = &buffer[strlen(output)-strlen(filler)+1];
						strncpy(output+i+strlen(filler),backbuffer,strlen(backbuffer));
						
						backbuffer = NULL;
						strset(buffer,NULL);
						strcpy(buffer,output);

						
						
					} else if((c != '\&') && (buffer[i] == c)) {
						printf("other found\n");
						strset(output,NULL);
						strncpy(output,buffer,i);
						strncpy( output+i,filler,strlen(filler)); 
						backbuffer = &buffer[strlen(output)-strlen(filler)+1];
						strncpy(output+i+strlen(filler),backbuffer,strlen(backbuffer));
						backbuffer = NULL;
						strset(buffer,NULL);
						strcpy(buffer,output);
					}
			
			}
		
		fprintf(fout,buffer);
		j++;
	} while (!feof(fin));
	fclose(fin);
	fclose(fout);
	remove(docname);
	rename("TMP.xml",docname);
}
[edited by - chbfiv on August 11, 2003 11:10:04 AM]

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement