Pregunta

I'm writing a program that is creating a lookup table from two tables that are in two separate files. When I read the first table, everything is read correctly. However, when I am reading the second file, fopen doesn't seem to open the whole file.

I say this because the compiler implements the _iobuf file structure and _cnt seems to be initialized a whole lot lower (_cnt = 530) than when initialized the first time(_cnt ~ 4096) and as I read from the file it decreases.

Here is a snippet of my code:

int vertical,horizontal,channels,count;
FILE *fp;

fp = fopen(filename,"r");
if(fp==NULL){
    cout << "File not found" << endl;
    return Mat();
}else{
    cout << "Opening and Reading " << filename << endl;
}

//Read header
fread(&count,4,1,fp);
cout << "ReadHistogram(): Count number is " << count;
if(count!= 5){
    cout << "Header file reads: " << count << endl;
    return Mat();
} 

//Read size
fread(&vertical,4,1,fp);
//cout << "ReadHistogram(): Vertical size:" << vertical <<endl;

fread(&horizontal,4,1,fp);
//cout << "ReadHistogram(): Horizontal size:" << horizontal <<endl;

fread(&channels,4,1,fp);
//cout << "ReadHistogram(): Channel size:" << channels<<endl;

//Create Mat array
int size[] = {vertical, horizontal, channels};
Mat histogram(3,size,CV_64F,Scalar::all(0));

//Read in array
count = 0;
for(int i=0;i<vertical;i++){
    for(int j=0;j<horizontal;j++){
        for(int k=0;k<channels;k++){
            double temp5;
            fread(&temp5,8,1,fp);
            histogram.at<double>(i,j,k) = temp5;
            if(count <= 300){
                cout << "Array(" << i+1 << "," << j+1 << "," << k+1 << ")" << "=" << histogram.at<double>(i,j,k) << endl;
                cout << "Temp5 is " << temp5 << endl;
            }
            count++;
        }
    }
}
cout << "Done reading " << filename << endl;
fclose(fp);
return histogram;

PS: I have been trying to look up what exactly is _cnt in the FILE structure and I can't find anything of the sort. I would appreciate any pointers anyone can give.

¿Fue útil?

Solución

It looks like you're reading a binary file in text mode. Try adding "b" to the flags in the fopen:

fp = fopen(filename, "rb");

Also check the return value of fread and if it's fewer bytes than you expected, check the ferror code.

Otros consejos

Your code looks fine, although you should check the return values of fread. If you're looking into the internals of FILE, you're only asking for confusion. _cnt probably has to do with how much of the file is buffered in memory. It is not part of the API, and is thus not documented and you should not look at it.

What is the problem, exactly? When does fread fail and what does it return?

Yea, I can read values and they are correct. The problem is it's that after a certain point, fread begins to return the same value.

I'm going to go out on a limb here with an answer rather than continued discussion; I'll delete if I'm off-base.

I expect what is happening is you have read the entire contents of your file however you have not stopped reading; subsequent calls to fread() are returning an error (which you aren't checking for) and not modifying the contents of your read buffer.

Either check the return of fread(), or call feof() to check your status, or both.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top