我读了一种特殊格式的二进制文件。我必须使用动态数组来读取大小未知的字符串。一切正常,但 valgrind 中存在错误。\0 的赋值没问题,没有它我试过了。我不知道还有什么问题。
int ReadInt(ifstream& i)
{
int x=0;
i.read((char*)&x,4);
return x;
}
bool BINtoCSV ( const char * inFileName, const char * outFileName )
{
ifstream i(inFileName,ios::binary|ios::in);
if(i.fail()) return false;
ofstream o(outFileName,ios::binary|ios::out);
if(o.fail()) return false;
char eater[4];
for(unsigned f=0;f<4;f++)eater[f]='\0';
int rows=0,inLine=0;
char c='k';
i.read(eater,1);//H
i.read(eater,4);//num
i.read((char*)&rows,4);//rows
i.read((char*)&inLine,4);//inlines
for(int a=0;a<rows;a++){
i.read((char*)&c,1);
if(c!='R') {if(a==0){i.close(); o.close(); return true;}i.close(); o.close();
return false;}
i.read(eater,4);
for(int b=0;b<inLine;b++)
{
for(unsigned f=0;f<4;f++)eater[f]='\0';
i.read((char*)&c,1);
if(c=='I') { o<<ReadInt(i)<<(!((b+1)%inLine)?'\n':';');}
else if(c=='S')
{
int l=0; i.read((char*)&l,4);
char* block=new char[l];
for(int a=0;a<l;a++) block[a]='\0';
i.read(block,l);
o<<block<<(!((b+1)%inLine)?'\n':';');
delete [] block;
}
else
{
i.close();
o.close();
return false;
}
}
}
i.close();
o.close();
return true;
}
有来自 valgrind 的日志示例。
Invalid read of size 1
at 0x4C2BFB4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4EC62E0: std::basic_ostream<char, std::char_traits<char> >& std::operator<<
<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char
const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
by 0x401841: BINtoCSV(char const*, char const*) (in /home/ondrnovy/Plocha/a.out)
by 0x401EA7: main (in /home/ondrnovy/Plocha/a.out)
Address 0x5a07683 is 0 bytes after a block of size 3 alloc'd
at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
by 0x40179F: BINtoCSV(char const*, char const*) (in /home/ondrnovy/Plocha/a.out)
by 0x401EA7: main (in /home/ondrnovy/Plocha/a.out)