我正在尝试使用相同的c代码在Windows(xp 32位)计算机上读取Linux机器(OpenSuse 11.2,64bit)上生成的二进制文件,这些代码只是在相应的机器上编译的。在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
}
免责声明:我没有编写此代码,我只是想尝试使用它。
答案 0 :(得分:2)
您是否指定“b”以二进制模式打开文件?尝试使用fopen(fname, "rb")
答案 1 :(得分:1)
如果您尝试直接加载,例如结构,不。一旦你切换编译器,绝对没有理由相信结构是逐字节的,与你在其他机器上的结构相同。即使在同一个CPU上,填充和对齐也可能不同。
我的意思是不要这样做:
struct foo my_foo;
fwrite(&my_foo, sizeof my_foo, 1, my_binary_file);
相反,分别对结构的每个字段进行序列化和反序列化。
答案 2 :(得分:0)
我可能在这里咆哮错误的树,但我认为
feof(file)=16
用作“if”的一部分的将生成错误,因为它通过 single equals运算符被用作/解释为函数的赋值 。它可能应该是:
if (feof(file)==16) /* setting aside the comparison to 16 rather than for 0 or non-zero */