如何使用FREAD和fwrite函数来读写二进制文件? [英] How to use fread and fwrite functions to read and write Binary files?

查看:198
本文介绍了如何使用FREAD和fwrite函数来读写二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜在我的项目,我已经读它在的形式传感器数据的.bin文件短(16位值)。我这样做是使用 FREAD 函数到缓冲区中,但我觉得读中未正确发生。我的意思的之间存在什么我写,我读中没有一致性。你们可以建议是怎么回事错在这里?这不是从我的项目我的code ......我只是想验证 FREAD FWRITE 此功能。

 #包括LT&;&stdio.h中GT;
无效的主要()
{
    FILE * FP = NULL;    短×〔10] = {1,2,3,4,5,6,5000,6,-10,11-};
    短期结果[10];    FP = FOPEN(C:\\\\ temp.bin,世行);    如果(FP!= NULL)
    {
        FWRITE(X,2 / * sizeof的(短)* / 10 / * 20/2 * / FP);
        倒带(FP);
        FREAD(结果,2 / * sizeof的(短)* / 10 / * 20/2 * / FP);
    }
    其他
        出口(0);    的printf(\\ nResult);
    的printf(\\ N%D,导致[0]);
    的printf(\\ N%D,结果[1]);
    的printf(\\ N%D,导致[2]);
    的printf(\\ N%D,导致[3]);
    的printf(\\ N%D,结果[4]);
    的printf(\\ N%D,造成[5]);
    的printf(\\ N%D,结果[6]);
    的printf(\\ N%D,导致[7]);
    的printf(\\ N%D,结果[8]);
    的printf(\\ N%D,结果[9]);    FCLOSE(FP)
 }

在我做的FREAD()(HEX值):

  temp.bin:
01 02 03 04 05 06 E1 8E 88 06 EF BF B6 0B ...

在我做的fwrite()

 标准输出:
结果
0
914
-28
-28714
-32557
1
512
-32557
908
914


解决方案

打开方式 W + (读,写)的文件。下面code工作:

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    FILE * FP = NULL;    短×〔10] = {1,2,3,4,5,6,5000,6,-10,11-};
    短期结果[10];
    INT I;    FP = FOPEN(temp.bin,W +);    如果(FP!= NULL)
    {
        FWRITE(X,的sizeof(短),10 / * 20/2 * / FP);
        倒带(FP);
        FREAD(结果的sizeof(短),10 / * 20/2 * / FP);
    }
    其他
        返回1;    的printf(结果\\ n);
    对于(I = 0; I&小于10;我+ +)
        的printf(%D =%d个\\ N,我,(int)的结果[I]);    FCLOSE(FP);
    返回0;
}

通过输出:

 结果
0 = 1
1 = 2
2 = 3
3 = 4
4 = 5
5 = 6
6 = 5000
7 = 6
8 = -10
9 = 11

Hi in my project I've to read a .bin file which has sensor data in the form of short(16 bit values). I'm doing this using fread function into a buffer, but I feel that the reading-in is not happening correctly. I mean there is no consistence between what I write and what I read in. Can you guys suggest what is going wrong here? This is not my code from my project... I'm only trying to verify the fread and fwrite functions here.

#include<stdio.h>
void main()
{
    FILE *fp = NULL;

    short x[10] = {1,2,3,4,5,6,5000,6,-10,11};
    short result[10];

    fp=fopen("c:\\temp.bin", "wb");

    if(fp != NULL)
    {
        fwrite(x, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
        rewind(fp);
        fread(result, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
    }
    else
        exit(0);

    printf("\nResult");
    printf("\n%d",result[0]);
    printf("\n%d",result[1]);
    printf("\n%d",result[2]);
    printf("\n%d",result[3]);
    printf("\n%d",result[4]);
    printf("\n%d",result[5]);
    printf("\n%d",result[6]);
    printf("\n%d",result[7]);
    printf("\n%d",result[8]);
    printf("\n%d",result[9]);

    fclose(fp)
 }

After I do the fread() (HEX values):

temp.bin:
01 02 03 04 05 06 e1 8e 88 06 ef bf b6 0b...

After I do the fwrite()

stdout:
Result
0
914
-28
-28714
-32557
1
512
-32557
908
914

解决方案

Open the file with mode w+ (reading and writing). The following code works:

#include<stdio.h>
int main()
{
    FILE *fp = NULL;

    short x[10] = {1,2,3,4,5,6,5000,6,-10,11};
    short result[10];
    int i;

    fp=fopen("temp.bin", "w+");

    if(fp != NULL)
    {
        fwrite(x, sizeof(short), 10 /*20/2*/, fp);
        rewind(fp);
        fread(result, sizeof(short), 10 /*20/2*/, fp);
    }
    else
        return 1;

    printf("Result\n");
    for (i = 0; i < 10; i++)
        printf("%d = %d\n", i, (int)result[i]);

    fclose(fp);
    return 0;
}

With output:

Result
0 = 1
1 = 2
2 = 3
3 = 4
4 = 5
5 = 6
6 = 5000
7 = 6
8 = -10
9 = 11

这篇关于如何使用FREAD和fwrite函数来读写二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆