Reading data from a binary file

This is a discussion on Reading data from a binary file within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to read time from a binary file, but I couldn't get the right value. Each ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    11

    Reading data from a binary file

    Hi all,

    I am trying to read time from a binary file, but I couldn't get the right value. Each record is 112 bytes in binary file.

    time format is YYYYDDDHHMMSSTHT
    YYYY = Year
    DDD = Day of the year
    HH = hour
    MM = minute
    SS = second
    T = tenth of the second
    H = Hundread of the second
    T = Thousand of the second.

    Here is my program

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    int main (int argc, char *argv[])
    {
    FILE *faatr;
    FILE *outfile;
    struct
    {
    char emapty[4];
    unsigned long id;
    char emapty1[12];
    char time[8];
    char empty2[74];
    unsigned short key;
    char empty3[8];
    } mytype;
    
    
    
    if (argc < 2)
    {
    
    printf ("Usage: %s key number\n", argv[0]);
    exit (0);
    
    }
    
    faatr = fopen("myfile", "rb");
    outfile = fopen("out.txt", "wb");
    
    
    if (!faatr)
    {
    printf ("File can not be opened for reading\n"); 
    }
    else
    {
    printf ("File opened.\n"); 
    printf("argument = %d\n",atoi(argv[1]));
    while(!feof(faatr))
    {
    
    fread(&mytype,sizeof(mytype),1,faatr);
    if(!feof(faatr))
    { 
    /*printf("1st value = %d\n",mytype.key);*/
    /* fprintf(outfile,"key valude= %u\n",mytype.key);*/
    if(mytype.key==atoi(argv[1]))
    {
    printf ("Key is found\n"); 
    fprintf(outfile,"freq = %u\n",mytype.id);
    fprintf(outfile,"start_time= %s\n",mytype.time);
    }
    
    }
    }
    }
    fclose(faatr); 
    fclose(outfile);
    return(0);
    }

    Here is binary file ( 112 bytes of each record) Seems like it is binary decimal file.

    I used od -x myfile to look at it.
    For the first record, time field starts 21 to 28 (1996 0301 9112 8831) and it seems like in YYYYDDDHHMMSSTHT format.
    How can I get this value?

    0000000 0001 f000 002f 7470 2020 2020 2020 2020
    0000020 2020 2020 1996 0301 9112 8831 1995 2681
    0000040 7575 3332 1995 2681 7580 8349 0000 0000
    0000060 2020 2020 2020 2020 5452 4149 4e30 3035
    0000100 0000 05dc 3033 3031 3931 3032 2020 2020
    0000120 2002 0001 0000 0000 0000 e040 01ff 0001
    0000140 0800 0000 0000 0018 0002 0000 0000 0000
    0000160 0000 a000 002f 7470 2020 2020 2020 2020
    0000200 2020 2020 1996 0301 9113 0348 1995 2681
    0000220 7581 2850 1995 2681 7581 7349 0000 0000
    0000240 2020 2020 2020 2020 5452 4149 4e30 3035
    0000260 0000 05dc 3033 3031 3931 3032 2020 2020
    0000300 2002 0002 0000 0000 0000 e040 02ff 0001
    0000320 0800 0000 0000 0001 0003 0000 0000 0000
    0000340 0004 7000 002f 7470 2020 2020 2020 2020
    0000360 2020 2020 1996 0301 9113 1565 1995 2681
    0000400 7584 8183 1995 2681 7592 3219 0000 0000
    0000420 2020 2020 2020 2020 5452 4149 4e30 3035
    0000440 0000 05dc 3033 3031 3931 3032 2020 2020
    0000460 2002 0003 0000 0000 0000 e040 02ff 0001
    0000500 0800 0000 0000 0002 0004 0000 0000 0000


    I will be really appreciate your help.

    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Hi Salem,

    It worked fine. Thanx.
    However, I have found that year is in A.D format. I have no idea about the A.D. format.
    In my binary file, first record's year is 1996. But If I convert to decimal it gives me 6550. Do you think I got the right value from the binary file?

    please let me know.
    Thanks again.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Hi,

    In this program, I also want to add path name(directory) where binary file is.

    Rightnow, I have binary file in same directory where my c program is.

    I wanna type pathname (directory for binary file) as an argument. and it goes to that directory and check for binary file. If it is there then procced otherwise gives message that no binary file found.

    I type
    a.out key /pathname

    so my usage will change to
    if (argc < 3)
    {

    printf ("Usage: %s key number and path name\n", argv[0]);
    exit (0);

    }

    How can I do that?

    I will be appriciate your help.
    Thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fopen(argv[2], "rb");
    This will assume that argv[2] is a string representing the actual file you want to open. It can have a pathname in it as well, so both of these work:

    a.out 123 myfile.bin
    a.out 123 /somedir/myfile.bin

    If you only want the pathname specified on the command line, you'll need a buffer to hold the path, then strcat() on the filename.

    [edit] Damn, beat!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Hi Salem,

    I used your code, but I am getting warning. Anyhow, can I get rid of it?


    "Test.c", line 100: warning: argument #1 is incompatible with prototype:
    prototype: pointer to uchar : "Test.c", line 6
    argument : pointer to char

    line6:
    char *bcd ( unsigned char *buff ) {

    line100:
    fprintf(outfile,"start_time= %s\n",bcd(mytype.start_time));

    Thanks for your help.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Thanks Salem. It worked.

    I need to little change my program. I added another time (time2) in my structure.


    struct {
    char emapty[4];
    unsigned long id;
    char emapty1[12];
    unsigned char time1[8];
    unsigned char time2[8];
    char empty2[66];
    unsigned short key;
    char empty3[8];
    } mytype;


    for example, for the first record, I am getting value for
    time1 is 1996030191128831
    time2 is 1995268175808349


    For time1,First it prints time value in YYYYDDDHHMMSSTHT, which I alreday got it then it prints in YYMMDD format.

    For time2, it prints time value in YYYYDDDHHMMSSTHT, which I alreday got it then it prints in HHMMSS format.

    How can I do it?

    I will be really appreciate your help.

    Thanks.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Hi Salem,

    Yes, I want convert time1 format from YYYYDDDHHMMSSTHT to YYMMDD (YearMonthDay).

    I got the time2 format.

    strcpy(tmp,bcd(mytype.time2));
    store[0] = tmp[7];
    store[1] = tmp[8];
    store[2] = tmp[9];
    store[3] = tmp[10];
    store[4] = tmp[11];
    store[5] = tmp[12];
    store[6] = '\0';
    fprintf(outfile,"tu = %s\n",store);

    How can I get the time1 format?

    Thanks for your help.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Thanks a lot guys.
    I solved my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading binary file with different data types
    By larne in forum C Programming
    Replies: 8
    Last Post: 07-29-2008, 11:12 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21