代码来自我的朋友。请看一下,并给我们一些建议。
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 );
}
....
}
我们在上面的方法中打印出来,为grpVolFileName赋值,结果看起来不错。这些值是某些文件的位置。然后调用以下方法使用上面的字符串指针的grpVolFileName数组。对于前4个好,然后打印输出显示目录无法正确打印,一些正方形打印出来并发生错误。但它并不总是排在第4位,有时它会在非常开始时发生。
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;
}
//.......
}
从上面的方法打印出来,如:
...
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.
表示在此过程中已更改grpVolFileName数组。它会导致这个if子句if( aStream_grpVolt[grpIndex][fileIndex].good() )
,返回flase并发生错误。
有任何意见吗?
答案 0 :(得分:2)
我很遗憾地说你朋友的代码真的很乱。我怀疑你在for循环中破坏了你的字符串。您可以尝试将其重写为:
在VoltageForDisplayAndAnalyze.cpp
:
void VoltageForDisplayAndAnalyze::MakeFileNameForGroupCell(int xgrpIndex)
{
int numOfFiles = (EEnd - EBegin + 1) / 255;
grpVolFileName[xgrpIndex] = new string[numOfFiles];
aStream_grpVolt[xgrpIndex] = new fstream[numOfFiles];
// The asserts are not needed: new will throw an exception
// if the allocation fails.
...
for (int fileIndex = 0; fileIndex < numOfFiles; fileIndex++)
{
// You should use std::string instead of C strings whenever
// possible (and it means almost always: std::string can
// interoperate with C strings).
string tempbuf = mpath;
// This is the C++ way of converting numbers to strings.
ostringstream oss;
oss << xgrpIndex;
string numChar = oss.str();
tempbuf += numChar;
grpVolFileName[xgrpIndex][fileIndex] = tempbuf;
}
...
}
// Remember to delete the arrays you have allocated with new:
// delete[] grpVolFileName[xgrpIndex];
// delete[] aStream_grpVolt[xgrpIndex];
虽然仍然不干净简洁(for循环中的代码非常冗余,我们可以使用std :: vector而不是C数组),但现在可以保证将此代码分配给grpVolFileName[xgrpIndex][fileIndex]
尝试运行此代码,看看您的错误是否已解决。
答案 1 :(得分:1)
我读了两遍你的问题,但我仍然不确定你想要实现的目标。据我所知,您实际上是遍历目录层次结构。它(几乎)与您正在处理的平台无关:可能有比C ++更好的工具。您可以更快,更轻松地敲打脚本,代码更短,正确执行相同任务。
答案 2 :(得分:1)