here, ill post the file class.
file.h
#ifndef _FILE_#define _FILE_const unsigned int FILE_PATH_SIZE = 400;const unsigned int FILE_NAME_SIZE = 200;const unsigned int FILE_EXT_SIZE = 5;const unsigned int FILE_ACCESS_MODE_SIZE = 5;#include <stdio.h>#include <string.h>class file{public: file(); file(char* filePath, char* accessMode); virtual ~file(); FILE* GetFilePointer();private: void OpenFile(); void CloseFile(); void GetFileNameFromPath(); void GetExtensionFromPath(); void DetermineFileSize(); FILE* fp; unsigned long fileSize; char filePath[FILE_PATH_SIZE]; char fileName[FILE_NAME_SIZE]; char fileExt[FILE_EXT_SIZE]; char fileAccessMode[FILE_ACCESS_MODE_SIZE]; bool fileOpen;};#endif //_file_
file.cpp
#include "file.h"//////////////////////////////////////////////////////////////////////////////////////////// Name: file()// Params: filePath - pointer to the incoming path// accessMode - pointer to the incoming access mode// Desc: Contructor for the file class. Checks the validity of the incoming strings,// sets the local file data tot eh incoming values and calls the open file func.//////////////////////////////////////////////////////////////////////////////////////////file::file(char* filePath, char* accessMode){ if ( (filePath != NULL) && (accessMode != NULL) ) { this->fileOpen = false; strcpy(&this->filePath[0], filePath); strcpy(&this->fileAccessMode[0], accessMode); OpenFile(); }}//////////////////////////////////////////////////////////////////////////////////////////// Name: ~file()// Params: N/A// Desc: Destructor for the file class. Calls the CloseFile() function when the // any objects of this class go out of scope.//////////////////////////////////////////////////////////////////////////////////////////file ::~file(){ CloseFile();}//////////////////////////////////////////////////////////////////////////////////////////// Name: OpenFile()// Params: N/A// Desc: After checking whether this file has already been opened, opens if it hasn't// been opened and sets the value of fileOpen accordingly.//////////////////////////////////////////////////////////////////////////////////////////void file::OpenFile(){ if (fileOpen == false) { fp = fopen(&filePath[0], &fileAccessMode[0]); if (fp != NULL) { fileOpen = true; GetFileNameFromPath(); GetExtensionFromPath(); DetermineFileSize(); } }}//////////////////////////////////////////////////////////////////////////////////////////// Name: GetFilePointer()// Params: N/A// Desc: This function returns pointer returns the member file pointer to the calling// class only if it is valid, otherwise NULL is returned.//////////////////////////////////////////////////////////////////////////////////////////FILE* file::GetFilePointer(){ if (fileOpen == true) { return fp; } return NULL;}//////////////////////////////////////////////////////////////////////////////////////////// Name: CloseFile()// Params: N/A// Desc: This function closes the file pointed to by the member file pointer only if// the file has been opened.//////////////////////////////////////////////////////////////////////////////////////////void file::CloseFile(){ if (fileOpen == true) { fclose(fp); if (fp == NULL) { fileOpen = true; } }}//////////////////////////////////////////////////////////////////////////////////////////// Name: GetFileNameFromPath()// Params: N/A// Desc: This function is used to extract the file name//////////////////////////////////////////////////////////////////////////////////////////void file::GetFileNameFromPath(){ unsigned long lengthOfPath = 0; unsigned long pathPosPtr = 0; lengthOfPath = strlen(&filePath[0]); for ( pathPosPtr = (lengthOfPath - 1); pathPosPtr >= 0; pathPosPtr--) { if ( filePath[pathPosPtr] == '\\') { strcat(&fileName[0], &filePath[pathPosPtr+1]); break; } }}//////////////////////////////////////////////////////////////////////////////////////////// Name: GetExtensionFromPath()// Params: N/A// Desc: This function is used to extract the file extension//////////////////////////////////////////////////////////////////////////////////////////void file::GetExtensionFromPath(){ unsigned long lengthOfPath = 0; unsigned long pathPosPtr = 0; lengthOfPath = strlen(&filePath[0]); fileExt[0] = NULL; for ( pathPosPtr = (lengthOfPath - 1); pathPosPtr >= 0; pathPosPtr--) { if ( filePath[pathPosPtr] == '.') { strcat(&fileExt[0], &filePath[pathPosPtr+1]); break; } }}//////////////////////////////////////////////////////////////////////////////////////////// Name: DetermineFileSize()// Params: N/A// Desc: This function is used to get the file size of the file.//////////////////////////////////////////////////////////////////////////////////////////void file::DetermineFileSize(){ fseek(fp, 0, SEEK_END); fileSize = ftell(fp);}
I added another contructor:
file();
and that seemed to solve the problem.
Is that what i should have done?
ace