Re: fread can not read particular data

From: Darrell Grainger (darrell_at_NOMORESPAMcs.utoronto.ca.com)
Date: 06/24/04

  • Next message: Darrell Grainger: "Re: faq & casting malloc()"
    Date: 24 Jun 2004 12:58:32 GMT
    
    

    On Wed, 23 Jun 2004, Meenakshi Matai wrote:

    > I am trying to read data from a .dat file using fread.
    >
    > This is how my syntax looks:
    > temp = fread(indata, sizeof(short int), 1, datafile);

    Insufficient information. Whenever you post a code snippet, give the data
    types and how they were initialized. This posting is missing the
    following:

    1) what is the data type of indata?
    2) how was indata initialized?
    3) what is the data type of datafile?
    4) how was datafile initialized?

    I can only guess that this is what you have:

    #include <stdlib.h>

            short int *indata; /* answers #1 */
            FILE *datafile; /* answers #3 */

            indata = malloc(sizeof(short int)); /* answers #2 */
            if(indata == NULL) exit(EXIT_FAILURE);

            datafile = fopen("filename.dat", "r"); /* answers #4 */
            if(datafile == NULL) exit(EXIT_FAILURE);

            fread(indata, sizeof(short int), 1, datafile);

    > I looked into the data file in Hex format and saw that everytime fread
    > finds the data "1A" it returns a non-zero value.
    >
    > I tried with another .dat file and the same thing happens.
    > I went in and manually changed the contents of the .dat file.
    > Everywhere I found a "1A" I changed it to "1B" ("1C", "1D", "0D") and
    > it works just fine.
    > Anything but "1A" works fine.
    >
    > Could you please suggest why this would be happening.

    The binary value 0x1A just happens to be the ASCII value CTRL-Z. On
    MSDOS/Windows systems, CTRL-Z is the EOF for text files.

    Problem: you have opened a binary file in text mode.
    Solution: open the binary file in binary mode.

    -- 
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to vice.president@whitehouse.gov
    

  • Next message: Darrell Grainger: "Re: faq & casting malloc()"

    Relevant Pages

    • Re: Mutli Type in same file
      ... then you can use a Random file and just offset ... by the number of records the header took up. ... header out to 1000 bytes and read that in via binary mode. ... using a binary file and your own read and write code is not ...
      (comp.lang.basic.visual.misc)
    • Print a string in binary format
      ... I have a binary file and wish to see the "raw" content of it. ... it in binary mode, and read one byte at a time to a variable, which ... but I just cannot find the appropriate method from the documentation. ...
      (comp.lang.python)
    • Re: H&S fread/fwrite
      ... Martin Ambuhl writes: ... I don't think your assertion (that every text file is a binary file) ... file in binary mode, or a binary file in text mode, will always fail. ... If you use an attribution line, ...
      (comp.lang.c)
    • Re: 0D after 0A in hex when writing a binary file
      ... > line feed and carriage return. ... (and those are zeros and not Os in the numbers). ... > binary mode. ... > I am using 010 Editor to edit the binary file. ...
      (comp.lang.cpp)