最近我一朋友被文件的读写所困扰,不同平台不同格式不同类型,C写M读、M写C读……
C就不说了,MATLAB就读文件的函数就有一堆:TEXTREAD, TEXTSCAN, STRREAD, DLMREAD, LOAD, SSCANF, FREAD等,每个参数也是各不相同。
要我说如果仅仅是自己个人使用,用作不同语言不同平台间传递数据,最好还是使用二进制格式,“0/1”是我们值得信赖的伙伴。所以在这里分享MATLAB和C语言,将不同类型数据保存为二进制的方法。
by HPC_ZY
C类型 | 对应M类型 | 字节数 | 位数 |
---|---|---|---|
char | char | 1 | 8 |
BYTE | uint8 | 1 | 8 |
int | int32 | 4 | 32 |
float | single | 4 | 32 |
double | double | 8 | 64 |
% 测试数据
A = [0,1,2,4,16,64,128,255]
%% byte
data = uint8(A);
% write
fp = fopen('.\data.byte','wb');
fwrite(fp,data);
fclose(fp);
% read
fp = fopen('.\data.byte','rb');
data = fread(fp,'bit8=>uint8');
fclose(fp);
%% int
data = int32(A);
% write
fp = fopen('.\data.int','wb');
fwrite(fp,data);
fclose(fp);
% read
fp = fopen('.\data.int','rb');
data = fread(fp,'bit8=>int32');
fclose(fp);
%% float
data = single(A);
% write
fp = fopen('.\data.float','wb');
fwrite(fp,data);
fclose(fp);
% read
fp = fopen('.\data.float','rb');
data = fread(fp,'bit8=>single');
fclose(fp);
%% double
data = single(A);
% write
fp = fopen('.\data.double','wb');
fwrite(fp,data);
fclose(fp);
% read
fp = fopen('.\data.double','rb');
data = fread(fp,'bit8=>double');
fclose(fp);
// 设置一个小的尺寸方便测试
int N = 16;
// 生成数据
BYTE *data = (BYTE*)malloc(N * sizeof(BYTE));
for (int i = 0; i < N; i++)
{
data[i] = i;
}
// 写文件
FILE* fp = fopen("D:\\data.BYTE", "wb");
fwrite(data, 1, N, fp);
fclose(fp);
// 读文件
BYTE *datain = (BYTE*)malloc(N * sizeof(BYTE));
fp = fopen("D:\\data.BYTE", "rb");
fread(datain, sizeof(BYTE), N, fp);
fclose(fp);
// 显示
for (int i = 0; i < N; i++)
{
printf("%d\n", data[i]);
}
// 生成数据
int *data = (int*)malloc(N * sizeof(int));
for (int i = 0; i < N; i++)
{
data[i] = i;
}
// 写文件
FILE* fp = fopen("D:\\data.int", "wb");
fwrite(data, 1, N, fp);
fclose(fp);
// 读文件
int *datain = (int*)malloc(N * sizeof(int));
fp = fopen("D:\\data.int", "rb");
fread(datain, sizeof(int), N, fp);
fclose(fp);
// 显示
for (int i = 0; i < N; i++)
{
printf("%d\n", data[i]);
}
// 生成数据
float *data = (float*)malloc(N * sizeof(float));
for (int i = 0; i < N; i++)
{
data[i] = i;
}
// 写文件
FILE* fp = fopen("D:\\data.float", "wb");
fwrite(data, 1, N, fp);
fclose(fp);
// 读文件
float *datain = (float*)malloc(N * sizeof(float));
fp = fopen("D:\\data.float", "rb");
fread(datain, sizeof(float), N, fp);
fclose(fp);
// 显示
for (int i = 0; i < N; i++)
{
printf("%.2f\n", data[i]);
}
// 生成数据
double *data = (double*)malloc(N * sizeof(double));
for (int i = 0; i < N; i++)
{
data[i] = i;
}
// 写文件
FILE* fp = fopen("D:\\data.double", "wb");
fwrite(data, 1, N, fp);
fclose(fp);
// 读文件
double *datain = (double*)malloc(N * sizeof(double));
fp = fopen("D:\\data.double", "rb");
fread(datain, sizeof(double), N, fp);
fclose(fp);
// 显示
for (int i = 0; i < N; i++)
{
printf("%.2f\n", data[i]);
}
data = logical([0,1,0,1,0,1,0,1]);
% write
fp = fopen('.\data.bin','wb');
fwrite(fp,data);
fclose(fp);