or change the pointer array to reflect the first instance 0....
Printable View
or change the pointer array to reflect the first instance 0....
char* file[] = {"test1.txt", "test2.txt", "test3.txt", "test4.txt", "test5.txt"};
Process()
{
char* ptr;
int i=0;
while(i<4)
{
ptr = file[i];
parse = parserFile(ptr); //inside this function I fopen and
//fclose the file already
remove(file[i]);
i++;
}
}
show the parserFile(ptr); code.
your while < 4 should read while < 5
parserFile(char* filename)
{
if ((file = fopen( (char *)filename,"r")) != NULL)
{
if ( stat( (char *) filename, &info ) == 0 )
{
ucMemorybuffer = (u8*) malloc( info.st_size );
if ( ucMemorybuffer != NULL )
{
myLoadedSrec = new SrecRep(filename);
if ( myLoadedSrec != NULL )
{
bprocessingError = false;
}
}
}
}
// Start reading the file....Go to the end!
while ( ((ret_fgets = fgets(line, MAXLINE, file)) != NULL) && (bprocessingError== false) )
{
if ( line[0] == 'S' ) // Does the line start with an S.
{
skipThisLine=false;// Assume that this is valid non comment srecord line
u32 i;
/* Figure out and handle the various srecord types. */
switch( line[1] )
{
case '1':
ptr = &line[2]; /* set pointer past 'S1' */
sscanf(ptr, "%2x%4x", &num, ¤tAddress);
ptr = &line[8]; /* set pointer to start of data bytes */
num -= 3; /* Subtract out the address and checksum */
break;
case '2':
ptr = &line[2]; /* set pointer past 'S1' */
sscanf(ptr, "%2x%6x", &num, ¤tAddress);
ptr = &line[10]; /* set pointer to start of data bytes */
num -= 4; /* Subtract out the address and checksum */
break;
// Skip the line no parsing necessary
default:
skipThisLine=true;
break;
}
if(skipThisLine != true) // This is not a line that we care about
{
//The while loop has been run before. if the following case is true
// a new data object needs to be created.
if(currentAddress != startAddress+currentStoreIndex)
{
/* Read in the data */
// opps the data is not contiguous or we hit the end of the file,
// so take what we have and put it into
// a data object, that will be stored as part of this srecord representation.
// Reset the start address to the new location.
// Set the current index to be 0
dotempDataObject = new dataObject();
if(dotempDataObject == NULL)
{
delete myLoadedSrec;
myLoadedSrec = NULL;
goto exit;
}
else //This is going to be a new block
{
s32 retval;
retval = dotempDataObject->addData(ucMemorybuffer,currentStoreIndex,startAddress);
// Give ownership of the data object to the srecord representation.
myLoadedSrec->addDataObject(dotempDataObject);
startAddress=currentAddress;
currentStoreIndex=0;
}
}
for (u32 i=0; i < num; i++)
{
sscanf(ptr, "%2X",&tempint); /* get next byte */
ucMemorybuffer[currentStoreIndex] = (u8) ( tempint & 0xFF);
currentStoreIndex++;
ptr+=2;
}
}
} /* Line does not start with the S */
} // end while loop
// The while loop has been quit. It could be that the end of the file has been reached
// or some other sever error has happened.
if( ferror(file) != 0 )
{
// An error occured
delete myLoadedSrec;
myLoadedSrec = NULL;
}
if( (feof(file) != 0) )
{
// This is the last data block read out of the file
dotempDataObject = new dataObject();
if(dotempDataObject != NULL)
{
dotempDataObject->addData(ucMemorybuffer, (currentStoreIndex),startAddress);
// Give ownership of the data object to the srecord representation.
myLoadedSrec->addDataObject(dotempDataObject);
}
else
{
delete myLoadedSrec;
myLoadedSrec = NULL;
}
}
exit:
if ( file != NULL )
{
fclose(file);
}
// Standard cleanup stuff
if(ucMemorybuffer != NULL)
{
free(ucMemorybuffer);
}
return myLoadedSrec;
}
fgets(line, MAXLINE, file))
how is line defined?
what is MAXLINE defined as?
is line defined as MAXLINE + 1??? fgets will append a null at the end of the read, so it will put at the most MAXLINE + 1 in line, and that is an array overwrite.
initial thoughts, on all the array work your doing is that your overwriting memory.
any work you do with ucMemorybuffer is ok, because you call free on it, and the DEBUG crt will check for the pads to make sure you haven't overwritten the array, and you've not mentioned hitting a hardcoded breakpoint.
I would also put breakpoints around the createtion of the file and the close of the file handle, make sure they are the same address, if not, you are indeed overwritting memory, and never freeing the handle, the other thing to do is, open the taskmanager, and the handle count column, watch your program as it runs, you'll see fairly quickly if your not closing the handle because of memory corruption.
The other thing is, make sure you have other application that has the files open, like a text editor etc.
i've tried both MAXLINE and MAXLINE+1. the latter gives me assertion failed error.
FILE *file a handler to the filename in parserFile()?
if I already fclose(file), doesn't it mean free the file from the handler?
I really have no idea how to solve this problem.
I have checked several time, as long as my application is opened,
I will get 'sharing violation' error if I manually delete those .txt files.
Something must be holding the file, but who?
:(:( :( :(
you tried MAXLINE+1 where? in the fgets? that's not where you need to do it, it's where you declare line....Quote:
Originally posted by nhcheng
i've tried both MAXLINE and MAXLINE+1. the latter gives me assertion failed error.
FILE *file a handler to the filename in parserFile()?
if I already fclose(file), doesn't it mean free the file from the handler?
I really have no idea how to solve this problem.
I have checked several time, as long as my application is opened,
I will get 'sharing violation' error if I manually delete those .txt files.
Something must be holding the file, but who?
:(:( :( :(
char line[MAXLINE+1];
maybe you could just zip it up (do a clean first) and post it as an attachment then I'll be able to tell you what's going on.
Thanks!
In fact, the SrecParser() is a predefined function. I don't wish to modify any codes in this function. I am just using it and put it in my application.
Thanks a lot!
well we are in the visual c++ forum, but what you posted isn't VC++ code.
but what I meant by MAXLINE+1 is this:
#define MAXLINE 512
s8 line[MAXLINE+1];
fgets(line, MAXLINE, file))
line needs the + 1
My application is using MFC. The operation underneath is C.
I added in +1 to line[MAXLINE + 1];
The file is still there without being removed.
:( :confused:
did you make MAXLINE 512 instead of 512 + 1???
what you posted for a zip is incomplete, I won't be able to compile it.
man I'm just reading thru this again...blah...distractions suck...
you said, you tried to delete it manually and you can't?
well when your program exits, it will close the file handles, so are you telling me you can't delete the file when your program is not running?
if that is true, it has nothing to do with your code (well unless your setting the attribute to read only)
check teh attributes on the file(s) are they read only? if so change them.
make sure no other program has them open!!!
until you can delete the file manually your code will never work.
I could not delete those files manually if my application is running.
It says sharing violation.
If my application is not running, the files could be deleted.
good I would have felt silly if it was as simple as the attribute.... :pQuote:
Originally posted by nhcheng
I could not delete those files manually if my application is running.
It says sharing violation.
If my application is not running, the files could be deleted.
well you need to do as I outlined above.
#define MAXLINE 512
s8 line[MAXLINE+1]
fgets(line,MAXLINE,file)
and set breakpoints where the handle to the file is created and where it is released.
also breakpoint and examine the string that you are using to pass to removefile
call
DWORD err = GetLastError();
after the remove(file)
and after the fclose(file)
or examine errno
let me know what those error codes are.
Your going to have to debug this...