开发者

Inconsistent behaviour with fopen in C/C++

开发者 https://www.devze.com 2023-02-28 09:35 出处:网络
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 correc

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);
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号