Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fread can not read particular data

Reply
Thread Tools

fread can not read particular data

 
 
Meenakshi Matai
Guest
Posts: n/a
 
      06-24-2004
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-24-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2004
On 23 Jun 2004 17:57:54 -0700, (E-Mail Removed) (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>>
 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      06-24-2004
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 (E-Mail Removed)
 
Reply With Quote
 
Meenakshi Matai
Guest
Posts: n/a
 
      06-24-2004
Keith Thompson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
open/fopen read/fread in multithreaded environment. Sheth Raxit C Programming 1 11-23-2007 07:07 AM
Re: fread not reading all information red floyd C Programming 4 04-13-2007 03:06 AM
fread not reading all information juleigha27@gmail.com C++ 3 04-12-2007 11:07 PM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM
read vs fread Patrice Kadionik C Programming 6 02-24-2005 08:42 PM



Advertisments