The code is from my friend. Please have a look at it and give us some suggestions.
in VoltageForDisplayAndAnalyze.h file
private:
string * grpVolFileName[ MAX_NUM_OF_GROUP ]; //for voltage files
fstream * aStream_grpVolt[ MAX_NUM_OF_GROUP ];
in VoltageForDisplayAndAnalyze.CPP file
void VoltageForDisplayAndAnalyze::MakeFileNameForGroupCell( int xgrpIndex )
{
int numOfFiles = (EEnd - EBegin + 1) / 255;
grpVolFileName[ xgrpIndex ] = new string[ numOfFiles ];
assert( grpVolFileName[ xgrpIndex ] != NULL );
aStream_grpVolt[xgrpIndex] = new fstream[ numOfFiles ];
assert( aStream_grpVolt[ xgrpIndex ] != NULL );
.....
for( int fileIndex=0; fileIndex<numOfFiles; fileIndex++ )
{
char * tempbuf = new char[256];
memcpy(tempbuf, mPath.c_str(), mPath.length() );
tempbuf[mPath.length()] = '\0';
char * numChar = new char[4];
itoa(xgrpIndex, numChar, 3 );
numChar[3] = '\0';
strcat(tempbuf, numChar );
grpVolFileName[ xgrpIndex ][ fileIndex ].assign(tempbuf );
}
....
}
we print out in above method which assign values to grpVolFileName, the result looks good. These values are location of some files. Then the following method is called to use above grpVolFileName array of string pointers. It is good for first 4 and then the print out show the directory cannot being print out correctly, some squares are print out and error happened. But it is not always at the 4th, sometime it happens at very begining.
void VoltageForDisplayAndAnalyze::SaveDataForGrpCell( int grpIndex, int fileIndex, int xBegin, int xEnd {
int i = 0;
char answer;
//save to hard disk
aStream_grpVolt[grpIndex][fileIndex].open( grpVolFileName[grpIndex][fileIndex].c_str() , fstream::out | std::ofstream::app );
cout << "open file: grpIndex= " << grpIndex <<" fileIndex= " << fileIndex << " : " << grpVolFileName[grpIndex][fileIndex] << " to save." << endl;
if( aStream_grpVolt[grpIndex][fileIndex].good() )
{
//.....
}
else
{
cout << "Cannot open file: " << grpVolFileName[grpIndex][fileIndex] << endl;
}
//.......
}
print out from above method like those:
...
open file: grpIndex= 2 fileIndex= 0 : C:\Workspace\UncusJava\DataFiles\spc1\2 to save.
open file: grpIndex= 3 fileIndex= 0 : C:\Workspace\UncusJava\DataFiles[][][][]4@ to save.
that means the grpVolFileName array has been changed in the process. It causes this if clause, if( aStream_grpVolt[grpIndex][fileIndex].good() )
, returns flase and error happen.
I searched entire solution (Visual Studio 2008), above two methods are only places use the grpVolFileName array of pointers. It is a single thread for this part.
Any comments?