Reading from binary file; fread problems?

This is a discussion on Reading from binary file; fread problems? within the C Programming forums, part of the General Programming Boards category; Folks - I've got a few Yahoo! Messenger .dat files that I'm trying to read. I found this site: http://www.0xcafefeed.com/2007/12/ya...e-file-format/ ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Reading from binary file; fread problems?

    Folks -

    I've got a few Yahoo! Messenger .dat files that I'm trying to read. I found this site:

    http://www.0xcafefeed.com/2007/12/ya...e-file-format/

    that describes the file format. Basically, you have four four-byte integers, then an xor'ed message payload. The payload is different for each message. The fourth integer gives you the length of the message payload in bytes.

    My goal is to read these messages in, de-XOR them, and then output them to a file. Reading the four integers is easy, but the idea fails when I get to reading the message itself. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct y_mess_header
    {
            int epoch_time;
            int throwaway;
            int sender;
            int length;
            char *text;
    };
    
    main()
    {
            int x,y;
            char z;
            struct y_mess_header message;
            FILE *fp;
    
            fp=fopen("./20081202-xxxxxx.dat","r");
            fread(&message,sizeof(message)-4,1,fp);
    
            message.text=(char *)malloc(message.length+1);  
    
            printf("Epoch time is: %d \n",message.epoch_time);
            printf("Throwaway is: %d \n",message.throwaway);
            printf("Sender is: %d \n",message.sender);
            printf("Message length is: %d \n",message.length);
            y=fread(&message.text,1,message.length,fp);
            printf ("Read: %d\n",y);
            printf("Array 0 is %c\n",message_text[0]);
    
    }
    After the fread() call, any access to the message_text array causes a segfault. I've tried to add a string terminator, which fails; printing one element of the array fails. I'm having trouble seeing what I'm doing wrong here. Does anyone have any idea?

    Thanks for the help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You probably need "rb" if you're going to read binarily from a file. (Otherwise some bytes will get reinterpreted.)

    Also message_text does not appear to exist.

    What value do you get for y?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > y=fread(&message.text,1,message.length,fp);
    Also, message.text is a pointer, so your use of & in this context is very wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Quote Originally Posted by Salem View Post
    >
    Also, message.text is a pointer, so your use of & in this context is very wrong.
    Ah, that was it. Didn't even cross my mind; I haven't touched pointers in years.

    Thanks to you and to tabstop for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 04:01 AM
  3. Reading a file in binary question
    By Dan17 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 01:28 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. problems reading from a text file
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 06:11 PM

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