17

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
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
user1190135
  • 215
  • 1
  • 2
  • 7
  • Have you tried closing and then reopening the file? I'm not sure what the contents of a file are if you write to it and then read to it immediately. You may have to close it first to make sure the data is flushed out? Also, make sure you open it for reading the second time around... – aardvarkk Feb 07 '12 at 16:35
  • Should really always use 'b' in file mode if you're going to use fread/fwrite. Lots of opportunities for screw ups or odd behavior reading text files. Most implementations do text translation in fread/fwrite but, as i said, this can cause problems (translated CR/LFs can cause more data to be written or less data to be read than you intended). Use fgets/fputs to read/write text files. – Tim Ring Jun 26 '19 at 10:39

2 Answers2

14

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
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
3

When you opened the file, you forgot to allow for reading:

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

Should be:

fp=fopen("c:\\temp.bin", "w+b");
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 2
    The mode `rwb` is not recognized. I think you mean `w+b` or `wb+`. – pmg Feb 07 '12 at 16:42
  • 2
    Well @Richard: it isn't Standard nevertheless. [see here](http://port70.net/~nsz/c/c99/n1256.html#7.19.5.3) or [in this PDF](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf). – pmg Feb 07 '12 at 16:44
  • The 'b' flag is ignored in `fopen()` under Mac OS X, but not under Microsoft's implementation. – trojanfoe Feb 07 '12 at 17:32