473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread can not read particular data

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.
Nov 14 '05 #1
4 6546
mm****@signalog ic.com (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_Keit h) ks***@mib.org <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.
Nov 14 '05 #2
On 23 Jun 2004 17:57:54 -0700, mm****@signalog ic.com (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>>
Nov 14 '05 #3
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(s hort int)); /* answers #2 */
if(indata == NULL) exit(EXIT_FAILU RE);

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

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 vi************@ whitehouse.gov
Nov 14 '05 #4
Keith Thompson <ks***@mib.or g> wrote in message news:<ln******* *****@nuthaus.m ib.org>...
mm****@signalog ic.com (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.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15578
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ==== The structure of the file is:
2
618
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0, but I don't know why it shouldn't work; but when I used ferror(stdin) and it gives 0 suggesting to me it read w/o problem, so I'm confused since I know there is information in the file, and it is allegedly reading from stdin, any help please? ...
8
17191
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
5
6353
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a time */ I would assume the latter form would be faster, or at least
20
7550
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
30
14283
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
8
11947
by: vippstar | last post by:
-- foo.c -- #include <stdio.h> int main(void) { struct { int a; int b; } foo; fread(&foo, sizeof foo, 1, stdin); return 0; } -- foo.c --
20
2047
by: xiao | last post by:
Hi~ every one~ I have a queston about fread function. if i have a code like this: (nscrdh and data are defined as two dementional arrays and both of them were stored in the same binary file) fread(&nscrdh,sizeof(nscrdh),1,in); fread(&data,sizeof(data),1,in); How can i know where the second fread start? Is it just start from the end of the first fread? Or somewhere else?
15
3186
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the full analysis start. My problem is that the FILE* I want do parse has been fopen'ed far away from where I am and I don't know in which MODE my FILE* has been opened. And additionally, my FILE* may not be a regular file, but a continuous
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.