我正在尝试编写一个程序来从二进制文件中获取输入,该文件中包含交替的Altitude和Airspeed数字的数据点,并将其转换为csv文件。数据需要从big endian到little endian的字节转换。当我运行我的代码时,我得到所有这些代码的相同数字,并且它不是用于打印输出的正确数字,并且除了奇怪的希腊字母之外它不会打印到csv文件。有人能告诉我我做错了什么吗?
代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
union byteswap
{
char c_num[4];
float f_num;
};
char temp;
union byteswap data;
int size;
int i;
struct time
{
//float time_s;
float Alt_ft;
float AS_knots;
//float mach_number;
};
struct time time_pt;
FILE *fp;
fp = fopen("C:\\Users\\daxon\\eclipse-workspace\\Flight-Data-Proj\\Alt_AS.dat", "r+b");
FILE *csv;
csv = fopen("C:\\Users\\daxon\\eclipse-workspace\\Flight-Data-Proj\\Flight-Data.csv", "w+");
if (fp== NULL)
{
fprintf(stderr, "Data file would not open");
return(-1);
}
fseek(fp, 0, SEEK_END);
size=ftell(fp);
fprintf(stdout, "The size of the file is %d\n", size);
fseek(fp,0,SEEK_CUR);
for (i = 0; i<= feof(fp); i++)
{
fread(&time_pt, 8, 1, fp);
fprintf(stdout, "Uswapped is %d\n", time_pt);
//byte swap
temp = data.c_num[0];
data.c_num[0] = data.c_num[3];
data.c_num[3]= temp;
temp = data.c_num[1];
data.c_num[1] = data.c_num[2];
data.c_num[2]= temp;
printf("Alt is %f, AS is %f\n", time_pt.Alt_ft, time_pt.AS_knots);
fwrite(&time_pt.Alt_ft, 4, 1, csv);
fwrite(&time_pt.AS_knots, 4, 1, csv);
}
fclose(fp);
fclose(csv);
fprintf(stdout, "Done reading files\n");
return 0;
}
输出:
文件大小为143208 Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
Uswapped是1986426061
Alt为1168552030029638100000000000000000.000000,AS为0.000000
完成阅读文件
我尝试了多种方法将文件拆分为两个结构变量,这让我最接近。
编辑:现在它只在文件更大的完成之前重复17次for循环。