Heap Corrupted in Writing to file(Urgent )

Goal of my program is :

1. To do some file operations.
2. if an error is thrown,i will capture the error and write into a file.For this i am maintaining error codes, and for corresponding error codes i have a text file, from which i will take the errors and write into a log file.

But the problem is that heap is getting corrupted after writing into the log file.

I am attaching the program, can you please let me know what is causing the error.

File.h
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
#ifndef FILE_H
#define FILE_H
#include <fstream>
#include<iostream>
#define WRITE_MODE      1
#define READ_MODE       2

class File
{
public:
	File(const char* pFileName,int open_mode);
	void Open();
	void Open(const char* pFileName,int open_Mode);
	void Close();
	bool IsFileExist();
	void CreateNewFileName();
	bool IsFileAlreadyOpen();
	void Write(const char* buf,const char* pErrorParameter1=NULL,const char* pErrorParameter2=NULL,const char* pErrorParameter3=NULL);
	std::fstream* GetFileHandle();
	void SetErrorFlag(){ mError = true;}
	bool IsErrorFlagSet(){return mError;}
	const char* GetFileNameAsCString(){ return mFileName.c_str();}
	~File();
private:
	std::string mFileName;
	int mFileMode;
	std::fstream    mOutputFileHandle;
	bool mError;

};
#endif 


File.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "File.h"
#include "error.h"
using namespace std;

File::File(const char* pFileName,int open_mode)
:mFileName(pFileName),mFileMode(open_mode),mError(false)
{
	
}

void File::Open()
{
	if(mFileMode == WRITE_MODE)
		mOutputFileHandle.open(mFileName.c_str(),ios::out | ios::app);
	else
		mOutputFileHandle.open(mFileName.c_str(),ios::in | ios::beg);
	if(mOutputFileHandle.fail())
	{
		cout<<"Error Opening the file"<<endl;
		exit(1);
	}
}

void File::Open(const char* pFileName, int open_mode)
{
	if(mOutputFileHandle.is_open())
	{
		mOutputFileHandle.close();
	}
	mFileName = pFileName;
	mFileMode = open_mode;
	Open();

}

bool File::IsFileExist()
{
	return mOutputFileHandle != NULL;
}
bool File::IsFileAlreadyOpen()
{
	return mOutputFileHandle.is_open();
}
void File::Write(const char* buf,const char* pErrorParameter1,const char* pErrorParameter2,const char* pErrorParameter3)
{
	if(IsFileAlreadyOpen())
	{
		if(!IsErrorFlagSet())
		{
			mOutputFileHandle<<buf<<"\n";
		}
		else
		{
			 char* pChar = (char*)(strstr(buf," '%1' "));
			if(pChar != NULL)
			   strcat((char*)buf,(char*)pErrorParameter1);//this is not working properly, but this is not an issue
			mOutputFileHandle<<buf<<"\n";
		}
		//mOutputFileHandle.close();
	}
	else
	{
		cout<<"File is not opened"<<endl;
	}

}
void File::Close()
{
	if(IsFileAlreadyOpen())
		mOutputFileHandle.close();
	else
		cout<<"File is not opened"<<endl;
}

std::fstream* File::GetFileHandle()
{
	return &mOutputFileHandle;
}
File::~File()
{
	mOutputFileHandle.close();
}

Logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef LOGGER_H
#define LOGGER_H
#include "error.h"
#include "File.h"
class Logger:public File
{
public:
	Logger();
	~Logger() { }
	void Read(int Code);
	std::string GetErrorString(){return errorString;}
private:
	std::string errorString;
};
#endif 

Logger.cpp
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
#include "Logger.h"
#include <string>

Logger::Logger():File("errors.txt",READ_MODE)
{
}
void Logger::Read(int code)
{
	char buffer[256];
	char codeBuffer[32];
	char* pCh;
	sprintf(codeBuffer,"%d",code);
	
	std::fstream* pHandle = GetFileHandle();
	while(!pHandle->eof())
	{
		pHandle->getline(buffer,256);
		pCh = strstr(buffer,codeBuffer);
		if(pCh != NULL)
		{
			pHandle->getline(buffer,256);
			errorString = buffer;
			break;
		}
	}
	/*
		After reading we should close the handel.
	*/
	//pHandle->close();
	
}


Error.h
1
2
3
4
5
6
7
8
#ifndef ERROR_H
#define ERROR_H
typedef enum error_names{
error_CannotOpenFile=1,
error_FileAlreadyOpened=2,
}errorCodes;

#endif 


error.txt
1
2
3
4
5
6
%%1%%
The file '%1' cannot be opened.
%%%0%%%
%%2%%
The file '%1' is already opened.
%%%0%%%


Main Progam :
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
// FilesOperation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
void ThrowException(errorCodes code,const char* pErrorParameter1=NULL,const char* pErrorParameter2=NULL,const char* pErrorParameter3=NULL)
{
	Logger pLogger;
	pLogger.Open();
	pLogger.Read((int)code);
	/*File pFile("ErrorFile.log",1);
	pFile.Open();
	pFile.SetErrorFlag();
	pFile.Write((pLogger.GetErrorString()).c_str(),pErrorParameter1,pErrorParameter2,pErrorParameter3);*/
	pLogger.Open("ErrorFile.log",1);
	pLogger.SetErrorFlag();
	pLogger.Write((pLogger.GetErrorString()).c_str(),pErrorParameter1,pErrorParameter2,pErrorParameter3);
	
}


int _tmain(int argc, _TCHAR* argv[])
{
	File pFile("log.txt",1);
	pFile.Open();
	char* buf="HI this is my sample programe";
	pFile.Write(buf);
	pFile.Close();
//just for checking whether the function is working or not
	ThrowException(error_CannotOpenFile,"log.txt");
	return 0;
}

Last edited on
let me know if there is problem in understanding the code.i have written this in visual studio 2005
Topic archived. No new replies allowed.