I am trying to read binary files generated on a Linux machine (OpenSuse 11.2, 64bit) on a Windows (xp 32bit) machine using the same c code, just compiled on the respective machines. On the windows machine I am using MinGW compiler.
I can read and write the files without error on the Linux machine however, when I try to read the files on a windows machine, the code seems to be failing on an end of file error.
specifically: feof(file)==16
Does anybody know what the problem might be? The bigger problem might be that fact that my C skills are not very strong…
I pulled some of the write code out (Well at least I think). Note: there is an option to write out ASCII files, however I prefer not to:
void write_node_data(void *nndes, void *x, void *y, void *z)
{
int tmpnndes;
float *tempx, *tempy, *tempz;
double *tempx64, *tempy64, *tempz64;
long tmpnndes64, longcount, totnodes;
strcpy(tmpname, "nodes ");
if (iflag64)
totnodes = tmpnndes64 = *((long *) nndes);
else
totnodes = tmpnndes = *((int *) nndes);
if (rflag64)
{
tempx64 = (double *) malloc(sizeof(double) * totnodes);
tempy64 = (double *) malloc(sizeof(double) * totnodes);
tempz64 = (double *) malloc(sizeof(double) * totnodes);
}
else
{
tempx = (float*) malloc(sizeof(float) * totnodes);
tempy = (float*) malloc(sizeof(float) * totnodes);
tempz = (float*) malloc(sizeof(float) * totnodes);
}
if (rflag64)
{
for (longcount = 0; longcount < totnodes; longcount++)
{
tempx64[longcount] = *((double *) x + longcount);
tempy64[longcount] = *((double *) y + longcount);
tempz64[longcount] = *((double *) z + longcount);
}
}
else
{
for (longcount = 0; longcount < totnodes; longcount++)
{
tempx[longcount] = *((float *) x + longcount);
tempy[longcount] = *((float *) y + longcount);
tempz[longcount] = *((float *) z + longcount);
}
}
if (filetype == IEEE_F)
fwrite(tmpname,sizeof(char),8,fp);
else
fprintf(fp,"nodes ");
if (iflag64)
{
if (filetype == IEEE_F)
fwrite(&tmpnndes64, INT64, 1, fp);
else
fprintf(fp,"%ld\n",tmpnndes64);
n_nodes = tmpnndes64;
}
else
{
if (filetype == IEEE_F)
fwrite(&tmpnndes, INT32, 1, fp);
else
fprintf(fp,"%d\n",tmpnndes);
n_nodes = tmpnndes;
}
if (rflag64)
{
if (filetype == IEEE_F)
{
fwrite(tempx64, FLOAT64, n_nodes, fp);
fwrite(tempy64, FLOAT64, n_nodes, fp);
fwrite(tempz64, FLOAT64, n_nodes, fp);
}
else
{
write_ascii_double(n_nodes, tempx64);
write_ascii_double(n_nodes, tempy64);
write_ascii_double(n_nodes, tempz64);
}
free(tempx64), free(tempy64), free(tempz64);
}
else
{
if (filetype == IEEE_F)
{
fwrite(tempx, FLOAT32, n_nodes, fp);
fwrite(tempy, FLOAT32, n_nodes, fp);
fwrite(tempz, FLOAT32, n_nodes, fp);
}
else
{
write_ascii_float(n_nodes, tempx);
write_ascii_float(n_nodes, tempy);
write_ascii_float(n_nodes, tempz);
}
free(tempx), free(tempy), free(tempz);
}
}
And the reading code:
if (lstructuredflag == 0 || lstructuredflag == 2)
{
lxic = (double *)malloc((lnodes)*sizeof(double));
lyic = (double *)malloc((lnodes)*sizeof(double));
lzic = (double *)malloc((lnodes)*sizeof(double));
if (lxic == NULL || lyic == NULL || lzic == NULL)
{
gmvrdmemerr();
return;
}
if (ftype != ASCII)
{
if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
{
tmpdouble = (double *)malloc((3*lnodes)*sizeof(double));
if (tmpdouble == NULL)
{
gmvrdmemerr();
return;
}
binread(tmpdouble,doublesize,DOUBLE,3*lnodes,gmvin);
ioerrtst(gmvin);
if (node_inp_type == 0) /* nodes type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpdouble[i];
lyic[i] = tmpdouble[lnodes+i];
lzic[i] = tmpdouble[2*lnodes+i];
}
}
if (node_inp_type == 1) /* nodev type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpdouble[3*i];
lyic[i] = tmpdouble[3*i+1];
lzic[i] = tmpdouble[3*i+2];
}
}
FREE(tmpdouble);
}
else
{
tmpfloat = (float *)malloc((3*lnodes)*sizeof(float));
if (tmpfloat == NULL)
{
gmvrdmemerr();
return;
}
binread(tmpfloat,floatsize,FLOAT,3*lnodes,gmvin);
ioerrtst(gmvin);
if (node_inp_type == 0) /* nodes type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpfloat[i];
lyic[i] = tmpfloat[lnodes+i];
lzic[i] = tmpfloat[2*lnodes+i];
}
}
if (node_inp_type == 1) /* nodev type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpfloat[3*i];
lyic[i] = tmpfloat[3*i+1];
lzic[i] = tmpfloat[3*i+2];
}
}
FREE(tmpfloat);
}
}
if (ftype == ASCII)
{
tmpdouble = (double *)malloc((3*lnodes)*sizeof(double));
if (tmpdouble == NULL)
{
gmvrdmemerr();
return;
}
rdfloats(tmpdouble,3*lnodes,gmvin);
if (node_inp_type == 0) /* nodes type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpdouble[i];
lyic[i] = tmpdouble[lnodes+i];
lzic[i] = tmpdouble[2*lnodes+i];
}
}
if (node_inp_type == 1) /* nodev type */
{
for (i = 0; i < lnodes; i++)
{
lxic[i] = tmpdouble[3*i];
lyic[i] = tmpdouble[3*i+1];
lzic[i] = tmpdouble[3*i+2];
}
}
FREE(tmpdouble);
}
}
the function Binread:
int binread(void* ptr, int size, int type, long nitems, FILE* stream)
{
int ret_stat;
#ifdef CRAY
float *floatptr, *floatbuf;
double *doubleptr, *doublebuf;
int tierr, ttype, tbitoff;
char *charptr;
int *intptr, *intbuf;
short *shortptr, *shortbuf;
tbitoff = 0; tierr = 0;
ret_stat = 0;
switch(type)
{
case CHAR:
charptr = (char *)ptr;
ret_stat = fread(charptr, size, nitems, stream);
break;
case SHORT:
ttype = 7;
shortbuf = (short *)malloc(size*nitems);
shortptr = (short *)ptr;
ret_stat = fread(shortbuf, size, nitems, stream);
tierr = IEG2CRAY(&ttype, &nitems, shortbuf, &tbitoff, shortptr);
free(shortbuf);
break;
case INT:
ttype = 1;
intptr = (int *)ptr;
intbuf = (int *)malloc(size*nitems);
ret_stat = fread(intbuf, size, nitems, stream);
tierr = IEG2CRAY(&ttype, &nitems, intbuf, &tbitoff, intptr);
free(intbuf);
break;
case FLOAT:
ttype = 2;
floatptr = (float *)ptr;
floatbuf = (float *)malloc(size*nitems);
ret_stat = fread(floatbuf, size, nitems, stream);
tierr = IEG2CRAY(&ttype, &nitems, floatbuf, &tbitoff, floatptr);
free(floatbuf);
break;
case DOUBLE:
ttype = 3;
doubleptr = (double *)ptr;
doublebuf = (double *)malloc(size*nitems);
ret_stat = fread(doublebuf, size, nitems, stream);
tierr = IEG2CRAY(&ttype, &nitems, doublebuf, &tbitoff, doubleptr);
free(doublebuf);
break;
case WORD:
intptr = (int *)ptr;
ret_stat = fread(intptr, size, nitems, stream);
break;
default:
fprintf(stderr,"Error: Cannot match input datatype.\n");
gmv_data.keyword = GMVERROR;
return;
}
if(tierr != 0)
{
fprintf(stderr,"Error: Cannot convert IEEE data to CRAY\n");
gmv_data.keyword = GMVERROR;
return;
}
return ret_stat;
#else
ret_stat = fread(ptr, size, nitems, stream);
if (swapbytes_on && type != CHAR && type != WORD)
swapbytes(ptr, size, nitems);
return ret_stat;
#endif
}
Disclaimer: I did not write this code I am simply trying to use it.
Did you specify “b” to open the file in binary mode? Try using
fopen(fname, "rb")