[英]Reading binary files generated on a linux machine on a windows machine
我正在嘗試使用相同的C代碼讀取在Windows(xp 32bit)計算機上的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
}
免責聲明:我沒有編寫此代碼,我只是在嘗試使用它。
您是否指定了“ b”以二進制模式打開文件? 嘗試使用fopen(fname, "rb")
如果您試圖直接加載例如結構,請不要。 切換編譯器后,絕對沒有理由相信該結構與另一台計算機上的字節對字節相同。 即使在同一CPU上,填充和對齊方式也可能不同。
我的意思是不要這樣做:
struct foo my_foo;
fwrite(&my_foo, sizeof my_foo, 1, my_binary_file);
而是分別對結構的每個字段進行序列化和反序列化。
我可能在這里吠錯了樹,但我認為
feof(file)=16
用作“ if”一部分的代碼將產生錯誤,因為它依靠單個 equals運算符被使用/解釋為對函數的賦值 。 它可能應該顯示為:
if (feof(file)==16) /* setting aside the comparison to 16 rather than for 0 or non-zero */
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.