PDA

Visualizza la versione completa : [C++] Errore su new() e delete()


kirakira93
12-06-2010, 15:01
Ecco qui il codice:

in un header ReadTextFromFile.h


//ReadTextFromFile.h
#include <stdio.h>
#include <cstdio>
#include <Windows.h>
#include <string.h>
#include <tchar.h>

typedef unsigned long long INT64BIT;

typedef struct _s_FileInfo
{
BOOL bFileExisting_ ; //out se TRUE il file esiste e viceversa
TCHAR * szTextInFile ; //out contenuto del file
TCHAR * szFilePath ; //in Path del file
TCHAR * szFileName ; //out Nome del file senza la path
TCHAR * szFileExtension ; //out Estensione del file
TCHAR * szOpenMode ; //in Da settare sempre "r", "r+" o "r+b"
INT64BIT i64bFileSize ; //Grandezza del file
}s_FileInfo ;

DWORD GetTextFromFile(s_FileInfo * s_FI);


Questa la struttura in cui verranno memorizzate le informazioni di un file

ed ecco invece il codice che riempe la struttura: tengo a sottolineare che non mi da errore di runtime sempre ma solo quando il processo terminato in modo anomalo, penso che comunque abbia scritto il codice in modo poco portabile.....:(

Dove scrivo //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< penso che ci sia un errore


#include "stdafx.h"
#include <stdio.h>
#include <cstdio>
#include <Windows.h>
#include <string.h>
#include <tchar.h>
#include "ReadTextFromFile.h"


#if !READ_TEXT_FROM_FILE_H
#define READ_TEXT_FROM_FILE_H 1

#define GTFF_SUCCESSED 0
#define GTFF_CANNOT_OPEN_FILE 1
#define GTFF_THE_FILE_DOESNT_EXIST 2
#define GTFF_CANNOT_READ_THE_FILE 3

#define DELETE_STRING ; /* \
{\
{\
s_FI->szFilePath = new TCHAR [1];\
s_FI->szFileName = new TCHAR [1];\
s_FI->szFileExtension = new TCHAR [1];\
szTextInFileBuffer = new TCHAR [1];\
szFgetsBuffer = new TCHAR [1];\
delete [] s_FI->szFilePath;\
delete [] s_FI->szFileName;\
delete [] s_FI->szFileExtension;\
delete [] szTextInFileBuffer\
delete [] szFgetsBuffer;\
}\
}*/
//qui da modificare....se tolgo il commento e lo metto come macromi da problemi.....
//contando che in un exe questa funzione pu essere utilizzata pi volte...sono un po confusionato!!!
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


DWORD GetTextFromFile(s_FileInfo * s_FI)
{

static BOOL bAlreadyCalled_ = FALSE;


bAlreadyCalled_ = TRUE;
TCHAR ** szFileName = &(s_FI->szFilePath);

//Check if the file is existing
WIN32_FIND_DATA wfd;
//WBox(s_FI->szFilePath);
HANDLE hFind = FindFirstFile(
s_FI->szFilePath,
&wfd
);
if (hFind == INVALID_HANDLE_VALUE)
{
s_FI->bFileExisting_ = FALSE;
MessageBox(NULL,_T("The file doesn't exist"),_T("Fatal Error"),MB_ICONERROR);
DELETE_STRING
return GTFF_THE_FILE_DOESNT_EXIST;
}

s_FI->bFileExisting_ = TRUE;

#define GTFF_SIZE_OF_FILE (wfd.nFileSizeHigh * (MAXDWORD+1) + wfd.nFileSizeLow)
s_FI->i64bFileSize = GTFF_SIZE_OF_FILE;

//Open the file

FILE * FFileToOpen;
FFileToOpen = fopen(s_FI->szFilePath,s_FI->szOpenMode);
if(FFileToOpen == NULL)
{
MessageBox(NULL,_T("Cannot open the file"),_T("Fatal Error"),MB_ICONERROR);
DELETE_STRING
return GTFF_CANNOT_OPEN_FILE;
}

//Read text form file
TCHAR * szFgetsBuffer;
szFgetsBuffer = new TCHAR [GTFF_SIZE_OF_FILE / sizeof(TCHAR)];
TCHAR * szTextInFileBuffer;
szTextInFileBuffer = new TCHAR [GTFF_SIZE_OF_FILE / sizeof(TCHAR)];
szTextInFileBuffer[0] = _T('\0');

for(int i = 0; i < 1;)
{
if(fgets(szFgetsBuffer,GTFF_SIZE_OF_FILE,FFileToOp en) == NULL)
break;
_tcscat(szTextInFileBuffer,szFgetsBuffer);
}

/*
int iResult = fread(szScanfBuffer, sizeof(TCHAR), GTFF_SIZE_OF_FILE, FFileToOpen);
if(
(iResult > GTFF_SIZE_OF_FILE /sizeof(TCHAR) && iResult != 0) ||
(iResult > GTFF_SIZE_OF_FILE /sizeof(TCHAR) && iResult < GTFF_SIZE_OF_FILE /sizeof(TCHAR))
)
MessageBox(NULL,"The file may be not corrispondent to the selected","Warning",MB_ICONWARNING);

if(iResult < 0 )
{
MessageBox(NULL,"Cannot read the file","Fatal error",MB_ICONERROR);
return GTFF_CANNOT_READ_THE_FILE;
}
*/

//Set file name into the structure

s_FI->szFileName = new TCHAR [_tcslen(s_FI->szFilePath) + 1];


for(int i = 0, j = 0; i < _tcslen(s_FI->szFilePath) + 1; i++)
{
if(s_FI->szFilePath[i] == _T('\0'))
{
s_FI->szFileName [j] = s_FI->szFilePath[i];
}
if(s_FI->szFilePath[i] == _T('\\'))
{
j = 0;
s_FI->szFileName[0] = _T('\0');
}
else if(s_FI->szFilePath[i] != _T('\\'))
{
s_FI->szFileName [j] = s_FI->szFilePath[i];
j++;
}
}

//Set file extension into the structure
s_FI->szFileExtension = new TCHAR [_tcslen(s_FI->szFilePath) + 1];


for(int i = 0, j = 0; i < _tcslen(s_FI->szFilePath) + 1; i++)
{
if(s_FI->szFilePath[i] == _T('\0'))
{
s_FI->szFileExtension [j] = s_FI->szFilePath[i];
}
if(s_FI->szFilePath[i] == _T('.'))
{
j = 0;
s_FI->szFileExtension[0] = _T('\0');
}
else if(s_FI->szFilePath[i] != '.')
{
s_FI->szFileExtension [j] = s_FI->szFilePath[i];
j++;
}
}
if(_tcscmp(s_FI->szFileExtension,s_FI->szFilePath) == 0)
s_FI->szFileExtension [0] = _T('\0');

s_FI->szTextInFile = new TCHAR [_tcslen(szTextInFileBuffer) + 1];

_tcscpy(s_FI->szTextInFile,szTextInFileBuffer);
fclose(FFileToOpen);
DELETE_STRING
return GTFF_SUCCESSED;
}

#endif //READ_TEXT_FROM_FILE_H



il codice facilmente leggibile......help me :cry: :cry: :cry: :cry:

Loading