fread can not read particular data

Discussion in 'C Programming' started by Meenakshi Matai, Jun 24, 2004.

  1. 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);

    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.

    Thanks a lot in advance,
    Meenakshi Matai.
     
    Meenakshi Matai, Jun 24, 2004
    #1
    1. Advertising

  2. (Meenakshi Matai) writes:
    > 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);
    >
    > I looked into the data file in Hex format and saw that everytime fread
    > finds the data "1A" it returns a non-zero value.


    fread() returns the number of items read; if the call above succeeds,
    it should return a non-zero value, specifically 1. I'm guessing you
    meant that it returns zero.

    Take a look at your call to fopen(). Did you open the file in binary
    mode?

    <speculation>
    You're running on an MS-DOS or Windows system, and you opened the file
    in text mode. The system uses an ASCII control-Z character (0x1A) as
    an EOF marker for text files. You may also find that any occurrences
    of "\r\n" (0X0D, 0x0A) are mapped to a single "\n" (0x0a). Opening
    the file with
    datafile = fopen("whatever.dat", "rb");
    should fix the problem.
    </speculation>

    Posting a small, complete, compilable program that illustrates the
    problem would have saved me the effort of using my uncanny
    clairvoyance to figure out what's really causing the problem.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 24, 2004
    #2
    1. Advertising

  3. On 23 Jun 2004 17:57:54 -0700, (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);


    Is indata an array or pointer? If it is a short as implied, then you
    should use &indata.

    >
    >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.
    >
    >Thanks a lot in advance,
    >Meenakshi Matai.




    <<Remove the del for email>>
     
    Barry Schwarz, Jun 24, 2004
    #3
  4. 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
     
    Darrell Grainger, Jun 24, 2004
    #4
  5. Keith Thompson <> wrote in message news:<>...
    > (Meenakshi Matai) writes:
    > > 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);
    > >
    > > I looked into the data file in Hex format and saw that everytime fread
    > > finds the data "1A" it returns a non-zero value.

    >
    > fread() returns the number of items read; if the call above succeeds,
    > it should return a non-zero value, specifically 1. I'm guessing you
    > meant that it returns zero.
    >
    > Take a look at your call to fopen(). Did you open the file in binary
    > mode?
    >
    > <speculation>
    > You're running on an MS-DOS or Windows system, and you opened the file
    > in text mode. The system uses an ASCII control-Z character (0x1A) as
    > an EOF marker for text files. You may also find that any occurrences
    > of "\r\n" (0X0D, 0x0A) are mapped to a single "\n" (0x0a). Opening
    > the file with
    > datafile = fopen("whatever.dat", "rb");
    > should fix the problem.
    > </speculation>
    >
    > Posting a small, complete, compilable program that illustrates the
    > problem would have saved me the effort of using my uncanny
    > clairvoyance to figure out what's really causing the problem.


    OK.
    Thanks for your help,
    Meenakshi.
     
    Meenakshi Matai, Jun 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    3
    Views:
    4,417
    Bruce Barker
    Nov 4, 2005
  2. Patrice Kadionik

    read vs fread

    Patrice Kadionik, Feb 24, 2005, in forum: C Programming
    Replies:
    6
    Views:
    5,425
    Patrice Kadionik
    Feb 24, 2005
  3. Replies:
    3
    Views:
    462
    CBFalconer
    Apr 13, 2007
  4. red floyd

    Re: fread not reading all information

    red floyd, Apr 12, 2007, in forum: C Programming
    Replies:
    4
    Views:
    354
    red floyd
    Apr 13, 2007
  5. Sheth Raxit
    Replies:
    1
    Views:
    505
    Richard Heathfield
    Nov 23, 2007
Loading...

Share This Page