Re: fread can not read particular data
From: Darrell Grainger (darrell_at_NOMORESPAMcs.utoronto.ca.com)
Date: 06/24/04
- Previous message: Emmanuel Delahaye: "Re: Callbacks in C"
- In reply to: Meenakshi Matai: "fread can not read particular data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Emmanuel Delahaye: "Re: Callbacks in C"
- In reply to: Meenakshi Matai: "fread can not read particular data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|