473,785 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why is fread forgetting where its position?

I found this very odd and maybe someone can explain it to me.

I was using fread to scan through a binary file and pull bytes out. In
the middle of a while loop, for no reason that i could discern, fread
all the sudden kept returning the same byte over and over as if it were
no longer advancing in the file. I used a hex editor to determine the
address of the last byte read in the file. CF was the last address, D0
was not ever read although my while loop kept running.

when i tried to use ftell to try to debug the problem, the program ran
fine. mmm, so i took the ftell out and the program ceased working
again. the ftell wasn't doing anything that i could tell. this is my
loop:

while (blah2!=3220) // I'm still working out bugs and using
temporary variables like "blah"
{
fread(&newval,B PS,1,WavInPtr); //put new byte in
newval

blah=oldval^new val[0];

ftell(WavInPtr) ; // fread forgets about itself
without this
if(blah < 0)
{
oldval = newval[0];
fprintf(WavOutP tr,"%ld \n",period_cntr );
period_cntr=0;

}

++period_cntr;

++blah2;
}

as you can see, ftell isn't attached to anything, it isn't doing
anything that i can see. but if i take it out, the program just runs
fine to a certain point and then stops making sense.

I'm compiling on Dev-c++ 4.9.9.2

I'd be glad to send the source code to anyone who wants to play with it
themselves. it requires a small wav file that comes with it.

Sep 29 '06 #1
13 3551
There are these things called "return values" from library routines.
It's often a good idea to see what they say, like at each and every
function call.

I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.

Sep 29 '06 #2
Ancient_Hacker wrote:
There are these things called "return values" from library routines.
It's often a good idea to see what they say, like at each and every
function call.
thanks much for the sarcasm. Actually, what i posted was the last step
in the debugging process, the bare minimum to get the program to
function. previously i had been printing the result of ftell after
each loop. I saw it incrementing, but i also saw the program function
properly. without this call to ftell (and without it only), the
program does not procede past a certain point in the file. it repeats
the same byte from the middle of the file (or, possibly, returns no
value at all, didn't test for that, oops).
I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.
Ah, i did open the file in text mode. Thank you for pointing that out.
This probably does affect things, however, it doesn't explain how a
call ftell allows the program to read all the way through the file. If
i had hit a data point that had the same bit pattern as an eof (which
it didn't, it hit decimal 31), would it continuoulsy return the same
byte or have no return value at all? or would it scroll off through
memory, reading the next consecutive bits?

Any insight into this quirk would be interesting to hear, thanks.

Sep 29 '06 #3
>There are these things called "return values" from library routines.
>It's often a good idea to see what they say, like at each and every
function call.

thanks much for the sarcasm. Actually, what i posted was the last step
in the debugging process, the bare minimum to get the program to
function. previously i had been printing the result of ftell after
each loop.
Did you also print the result of fread() after you called it?
>I saw it incrementing, but i also saw the program function
properly. without this call to ftell (and without it only), the
program does not procede past a certain point in the file. it repeats
the same byte from the middle of the file (or, possibly, returns no
value at all, didn't test for that, oops).
If fread() returns 0 or -1, DON'T EVEN LOOK at the value in the buffer.
>I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.

Ah, i did open the file in text mode. Thank you for pointing that out.
This probably does affect things, however, it doesn't explain how a
call ftell allows the program to read all the way through the file. If
i had hit a data point that had the same bit pattern as an eof (which
it didn't, it hit decimal 31), would it continuoulsy return the same
byte or have no return value at all? or would it scroll off through
memory, reading the next consecutive bits?
fread() returns an integer value. It cannot return "no return value at all".

Sep 30 '06 #4
hey gordon,
Did you also print the result of fread() after you called it?
If fread() returns 0 or -1, DON'T EVEN LOOK at the value in the buffer.
yes, i did. or rather, i put fread into a variable and then printed
that valiable. I kept geting 31.
fread() returns an integer value. It cannot return "no return value at all".
I was pretty sure that this was the case. but since my integer that
fread was retuned into kept giving the same response, the two
possiblities were 1) fread keeps giving the same byte or 2) fread isnt
writing into the memory space of that variable.

The real question is still how does a call to ftell affect the way
fread works, either by affecting the file pointer, or some buffer or
something. atleast, thats what seems to me to be happening, because
adding and subtracting that one call to ftell drastically changes the
output of my program, and the input is not changing at all.

thanks

Oct 1 '06 #5
010 010 wrote:
The real question is still how does a call to ftell affect the way
fread works, either by affecting the file pointer, or some buffer or
something. atleast, thats what seems to me to be happening, because
adding and subtracting that one call to ftell drastically changes the
output of my program, and the input is not changing at all.
You're seeing undefined behavior. Most likely because you did something to
create a situation that invokes undefined behavior. ftell() does not affect
fread().

Just give us the whole function source that we can work with or pull things up
in a debugger of some sort - because this is shooting in the dark at this
point unfortunately.
Oct 1 '06 #6
I still havent seen any unequivocal statement that you're printing out
the value that fread returns as it's function value. NOT the data
value that it's supposed to return in the first parameter's address.
You shouldnt look at that until you've verified that indeed fread says
it returned something, as indicated by the value it returns.

For extra info, clear out the buffer before the fread, like, put a '?'
into it. I suspect fread will do nothing in that last case and you'll
see a '?' still sitting in the buffer.

Oct 1 '06 #7
>Did you also print the result of fread() after you called it?
>If fread() returns 0 or -1, DON'T EVEN LOOK at the value in the buffer.

yes, i did. or rather, i put fread into a variable and then printed
that valiable. I kept geting 31.
ret = fread(&newbuf, 1, 1, f);

In the above statement, the return value of fread() has everything to do
with the value of ret and nothing whatever to do with what's in newbuf.
You should not look at any values in newbuf until you have verified that
ret 0.

If your code contains a statement like:

fread(&newbuf, 1, 1, f);

you obviously didn't use the return value of fread(). Replace this statement
with:
remove(__FILE__ );

Some bad code should be shot on sight.
Oct 1 '06 #8

010 010 wrote:
hey gordon,
Did you also print the result of fread() after you called it?
If fread() returns 0 or -1, DON'T EVEN LOOK at the value in the buffer.

yes, i did. or rather, i put fread into a variable and then printed
that valiable. I kept geting 31.
fread() returns an integer value. It cannot return "no return value at all".

I was pretty sure that this was the case. but since my integer that
fread was retuned into kept giving the same response, the two
possiblities were 1) fread keeps giving the same byte or 2) fread isnt
writing into the memory space of that variable.

The real question is still how does a call to ftell affect the way
fread works, either by affecting the file pointer, or some buffer or
something. atleast, thats what seems to me to be happening, because
adding and subtracting that one call to ftell drastically changes the
output of my program, and the input is not changing at all.

thanks
At a guess. You are stomping on memory somewhere before the fread
loop.
This memory stomp causes fread to fail. You do not check the
return value of fread and do not know it has failed. When fread fails
it does nothing to the input buffer, so the value that you are looking
at does not change (To check this try resetting newval between calls
to fread). The ftell call either sets the stomped on memory to
something less harmfull, or changes the memory layout in such
a way that the place that gets stomped is different (have you tried
using some other function than ftell, or changing optimization levels?)
The problem probably has little to do with fread or ftell. Try to
find
the memory stomp (do you have any memory debugging tools?)

-William Hughes

Oct 2 '06 #9
Groovy hepcat 010 010 was jivin' on 29 Sep 2006 13:05:47 -0700 in
comp.lang.c.
why is fread forgetting where its position?'s a cool scene! Dig it!
>I found this very odd and maybe someone can explain it to me.

I was using fread to scan through a binary file and pull bytes out. In
the middle of a while loop, for no reason that i could discern, fread
all the sudden kept returning the same byte over and over as if it were
no longer advancing in the file. I used a hex editor to determine the
address of the last byte read in the file. CF was the last address, D0
was not ever read although my while loop kept running.

when i tried to use ftell to try to debug the problem, the program ran
fine. mmm, so i took the ftell out and the program ceased working
again. the ftell wasn't doing anything that i could tell. this is my
loop:

while (blah2!=3220) // I'm still working out bugs and using
temporary variables like "blah"
{
fread(&newval,B PS,1,WavInPtr); //put new byte in
newval

blah=oldval^new val[0];

ftell(WavInPtr) ; // fread forgets about itself
without this
if(blah < 0)
{
oldval = newval[0];
fprintf(WavOutP tr,"%ld \n",period_cntr );
period_cntr=0;

}

++period_cntr;

++blah2;
}
Ye gads! Show us something that will compile. Make it legible.
Indent consistently, and not too far at each level. Two to four spaces
per indentation level should be fine. Don't use // comments here. They
have a nasty tendancy to wrap, making it harder for us to copy, paste
and compile your code. Use /* */ comments only, when posting here.
Show us the smallest *complete* program that demonstrates the
problem. By "complete" I mean something you expect to compile and run.
Cut out everything extraneous, anything that does not demonstrate the
problem. (And do try to compile the cut-down version of your code.)
Explain precisely but concisely what you expect the code to do, as
well as what it actually does.
Show us the data you're attempting to work on. If it's non-textual
data, attempt to provide some textual interpretation of this (eg., a
hex dump). Of course, if your dataset is large, you may need to
truncate it.
Without being provided all this information, all we can do is guess
what might be wrong. Remember, you want our help; so you're going to
have to do the work. Help us to help you. You're not likely to get
much help if you just say, effcetively, "Here's an uncompilable,
incomplete and barely legible snippet of code. Why doesn't it work?".
>as you can see, ftell isn't attached to anything, it isn't doing
anything that i can see. but if i take it out, the program just runs
fine to a certain point and then stops making sense.

I'm compiling on Dev-c++ 4.9.9.2

I'd be glad to send the source code to anyone who wants to play with it
themselves. it requires a small wav file that comes with it.
Let me guess. You've opened the file in text mode?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Oct 2 '06 #10

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

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
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:
10
4193
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like this: 456 5 1.txt%&'
6
19463
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose, fread, swrite, fcntl... Family read and Co:
2
5611
by: Fernando Barsoba | last post by:
Hi all, I'm trying to read from a binary file, and I'm using 'fread()' as indicated in function 'getfile()'. The variable 'bytesread' shows that I have read the 557,000 bytes from the file, but because the file has a 00 at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17 bytes. So, it's weird, because fread() seems to put 550,000 bytes in the buf variable, but after returning from the function, only bytes up to EOF...
2
6322
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
5
6355
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
7551
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...
4
3372
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and i'm experiencing unexpected behavior using fread with a binary file. Look at this code: i have a jpeg image of 2290 bytes long, but fread cannot read it correctly, like fgetc does:
0
10147
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
9947
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
8971
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...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.