[使用工具]Microsoft Visual Studio .NET 2003
又有問題要麻煩各位前輩嚕
題目要求:讀取Binary檔中的資料再拿來做其他功能[功能就不麻煩各位]
問題點:目前卡在fread[只能用fread]函數讀取檔案時無法停止,會讀到不知哪邊去最後掛
網路查資料是用EOF沒錯呀@@"
希望各位可以解惑,謝謝
附上原始資料檔[txt檔]跟Binary檔[bin檔]
程式碼:
語法:
#include <stdio.h>
#define MAX 100
#define NL 25
#define IL 80
typedef struct
{
char name[NL];
int speed;
double wing,length;
char introduce[IL],mc;
}dataread;
void main()
{
dataread data[MAX];
FILE *inp;
char state;
int i=0;
inp=fopen("input.bin","rb");
do
{
i++;
state=fread(&data[i],sizeof(data),1,inp);
printf("%s %d %lf %lf %c %s\n",data[i].name,data[i].speed,data[i].wing,data[i].length,data[i].mc,data[i].introduce);
}while(state!=EOF);
fclose(inp);
}
推敲到現在,我覺得我Binary轉檔時可能也是錯誤原因之ㄧ,所以也附上轉檔的程式碼
程式碼:
語法:
#include <stdio.h>
#include <string.h>
#define NL 25
#define IL 80
typedef struct
{
char name[NL];
int speed;
double wing,length;
char introduce[IL],mc;
}dataread;
void main()
{
dataread data;
FILE *inp,*outp;
char *state;
int i=0;
data.wing=0.0;
data.length=0.0;
inp=fopen("input.txt","r");
outp=fopen("input.bin","wb");
for(state=fgets(data.name,NL,inp);
state!=0;
state=fgets(data.name,NL,inp))
{
if(strlen(data.name)==1)
fgets(data.name,NL,inp);
if(data.name[strlen(data.name)-1]=='\n')
data.name[strlen(data.name)-1]='\0';
fscanf(inp,"%d %lf %lf %c\n",&data.speed,&data.wing,&data.length,&data.mc);
for(i=0;i<IL;i++)
data.introduce[i]='\0';
state=fgets(data.introduce,IL,inp);
if(data.introduce[strlen(data.introduce)-1]=='\n')
data.introduce[strlen(data.introduce)-1]='\0';
fwrite(&data,sizeof(data),1,outp);
}
printf("已經轉換完成\n");
fclose(inp);
fclose(outp);
}