C/C++ 中与 fopen 的行为不一致;
我正在使用一个可以多次打开同一文件的库。它检查文件的标头以确保其格式正确。前 1212 次打开文件时,它的行为正确。第1213次,从文件中读出的字节不同。谁能建议为什么会发生这种情况?
不幸的是,我无法制作一个可重现的小示例 - 并且需要 20 分钟才能完成这一点。所以我想知道 fopen 是否有任何我可能错过的微妙之处,或者其他可能对这个执行产生影响的东西。
代码如下。创建了该类的许多实例,并且每个实例都使用相同的文件名调用了initialise()。前1212次,输出为:
Expecting: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
Got: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
最后一次得到:
Expecting: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
Got: ' lon lat year
函数如下:
class Archive {
private:
FILE* pfile;
<snip>
bool initialise(char* filename) {
int i;
unsigned char* pheader;
if (pfile) fclose(pfile);
pfile=fopen(filename,"rb");
if (!pfile || pfile == NULL ) {
printf("Could not open %s for input\n",filename);
return false;
}
pheader=new unsigned char[CRU_1901_2002_HEADERSIZE-4];
if (!pheader) {
printf("Out of memory\n");
fclose(pfile);
pfile=NULL;
return false;
}
::rewind(pfile);
fread(pheader,CRU_1901_2002_HEADERSIZE-4,1,pfile);
printf( "Expecting: '%s'\n", CRU_1901_2002_HEADER);
for( int j = 0; j < CRU_1901_2002_HEADERSIZE-4;j++ )
printf( "%c", CRU_1901_2002_HEADER[j]);
printf( "\nGot: '%s'\n", pheader);
for( int j = 0; j < CRU_1901_2002_HEADERSIZE-4;j++ )
printf( "%c", pheader[j]);
printf( "\n");
for (i=0;i<CRU_1901_2002_HEADERSIZE-4;i++) {
if (pheader[i]!=CRU_1901_2002_HEADER[i]) {
fclose(pfile);
pfile=NULL;
delete pheader;
return false;
}
}
delete pheader;
::rewind(pfile);
fseek(pfile,CRU_1901_2002_HEADERSIZE+CRU_1901_2002_DATA_LENGTH*CRU_1901_2002_NRECORD,SEEK_CUR);
recno=0;
iseof=false;
return true;
}
public:
Archive() {
pfile=NULL;
}
Archive() {
if (pfile) fclose(pfile);
}
I'm working with a library which opens the same file many times. It checks the header of the file to make sure that it is the correct format. The first 1212 times it opens the file, it behaves correctly. The 1213th time, the bytes read out from the file are different. Can anyone suggest why this might be happening?
Unfortunately I can't make a small reproducible example - and it takes 20 minutes to run through to this point. So I'm wondering if there are any subtleties of fopen which I might have missed, or something else which might have a bearing on this execution.
The code is below. Many instances of the class are created, and each on has initialise() called with the same filename. The first 1212 times, the output is:
Expecting: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
Got: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
The last time I get:
Expecting: '?'
?lon-1800????%@LYB1800????%@LYB100????%@LYB
lat-900??p-2?%@HYB900??p-2?%@HYB10??p-2?%@HYB
? soilcode0 ?? ?-2?&@AYB12 ?? ?-2?&@AYB1 ?? ?-2?&@AYBmtemp-600??x.2?&@6YB600??x.2?&@6YB10??x.2?&@6YB
?mprec0???H2?&@.YB99999???H2?&@.YB1999???H2?&@.YB?msun0???A2?&@%YB1000???A2?&@%YB100???A2?&@%YB
?
Got: ' lon lat year
The function is as follows:
class Archive {
private:
FILE* pfile;
<snip>
bool initialise(char* filename) {
int i;
unsigned char* pheader;
if (pfile) fclose(pfile);
pfile=fopen(filename,"rb");
if (!pfile || pfile == NULL ) {
printf("Could not open %s for input\n",filename);
return false;
}
pheader=new unsigned char[CRU_1901_2002_HEADERSIZE-4];
if (!pheader) {
printf("Out of memory\n");
fclose(pfile);
pfile=NULL;
return false;
}
::rewind(pfile);
fread(pheader,CRU_1901_2002_HEADERSIZE-4,1,pfile);
printf( "Expecting: '%s'\n", CRU_1901_2002_HEADER);
for( int j = 0; j < CRU_1901_2002_HEADERSIZE-4;j++ )
printf( "%c", CRU_1901_2002_HEADER[j]);
printf( "\nGot: '%s'\n", pheader);
for( int j = 0; j < CRU_1901_2002_HEADERSIZE-4;j++ )
printf( "%c", pheader[j]);
printf( "\n");
for (i=0;i<CRU_1901_2002_HEADERSIZE-4;i++) {
if (pheader[i]!=CRU_1901_2002_HEADER[i]) {
fclose(pfile);
pfile=NULL;
delete pheader;
return false;
}
}
delete pheader;
::rewind(pfile);
fseek(pfile,CRU_1901_2002_HEADERSIZE+CRU_1901_2002_DATA_LENGTH*CRU_1901_2002_NRECORD,SEEK_CUR);
recno=0;
iseof=false;
return true;
}
public:
Archive() {
pfile=NULL;
}
Archive() {
if (pfile) fclose(pfile);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你确定第1213位有数据吗?或者,这些数据正确吗?
建议您挂载一个超过1213条记录的文件,进行测试,确认该位置是否存在读取错误。
Are you sure that there is data in the 1213th position? or, are these data are correct?
I suggest you mount a file with more than 1213th records and do a test to confirm if there is a read error or not in this position.
事实证明,这是由于打开的文件太多造成的。在其他地方更改程序以打开更少的文件可以修复此问题。
检查 fread 返回 1,最后一个除外,它返回 0。
但是,我不明白为什么 fopen 在无法打开文件时返回一个非空文件指针。在测试代码中,它返回 NULL,然后按预期捕获该值。
It turns out this is down to too many files being open. Changing the program elsewhere to open less files fixes it.
Checking fread returns 1, except for the last one, where it returns 0.
However, I don't understand why fopen gives back a non-null file pointer when it can't open the file. In test code, it returns NULL, which is then caught as expected.