fread can not read particular data

M

Meenakshi Matai

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.
 
K

Keith Thompson

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.
 
B

Barry Schwarz

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>>
 
D

Darrell Grainger

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.
 
M

Meenakshi Matai

Keith Thompson said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top