我正在尝试使用相同的 c 代码读取在 Windows(xp 32 位)机器上的 Linux 机器(OpenSuse 11.2、64 位)上生成的二进制文件,这些文件只是在各自的机器上编译的。在 Windows 机器上,我使用的是 MinGW 编译器。
我可以在 Linux 机器上毫无错误地读取和写入文件,但是,当我尝试在 Windows 机器上读取文件时,代码似乎因文件错误结束而失败。
具体来说:feof(file)==16
有谁知道问题可能是什么?更大的问题可能是我的C技能不是很强......
我提取了一些写代码(至少我认为)。注意:有一个写出 ASCII 文件的选项,但我不喜欢:
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);
}
}
和阅读代码:
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);
}
}
函数 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
}
免责声明:我没有编写此代码,我只是想使用它。