fread problem?

Reply

Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

fread problem?

 
0
  #1
Feb 24th, 2004
Hi. Can anyone help me to distinguish the reason behind this case? Thanks a lot.
I initialized the following variable: unsigned char temp=0;
In the following statement block, I managed to get the results that I wanted to be displayed on the first go (traversing the while loop on the first run).
However, on the 2nd run, the temp being printed equals to the last temp printed in the first run.

while
{
// other print statements
// statement to get rlen
for(i=0; i<rlen; i++)
{
fread( (char*) &temp, sizeof(temp), 1 ,input);
printf("%x ", temp);
}
}


I've thought of it might be because of pointer problems? I am not sure. I am not any good at this, just a beginner. Please help. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: fread problem?

 
0
  #2
Feb 24th, 2004
  1. for (int i = 0; i < rlen; i++)
  2. {
  3. printf("%x\n", fgetc(input));
  4. }

do it that way
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: fread problem?

 
0
  #3
Feb 24th, 2004
Thanks a lot for your reply.
I've tried and hey, the output differs, however it's still not what I expected it to be.

The first loop:
  1. a1 b3 8 9a // which is correct

The second loop:
  1. 9a 9a 9a 9a // before r0ckbaer's advice

  1. ffffff // after applying to r0ckbaer's advice
  2. ffffff
  3. ffffff
  4. ffffff
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: fread problem?

 
0
  #4
Feb 24th, 2004
Well, normally fgetc gets one char per loop (in the above example) and increments the file pointer with 1, so:
a1 b3 8 9a
would be a result after 4 loops.
Be more specific on how you want the output to look like and if possible paste a snapshot of the hexdump of lets say the first 50 bytes of the file u want to analyze.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: fread problem?

 
0
  #5
Feb 25th, 2004
Well, it's a project for computer networks actually.
I am to capture packets using a program called windump, and then decode each field of information in their respective ethernet frame, ip frame, tcp/udp frame.
It seems that the results is not stable, in some packets, I could get the correct output, while not in some other packets.
Is it always like that? (Now, it seems like it has to be in the 'networking' topic & not c already!)
This is because, I strongly feel that there is no problem with the codes. I've got a comment from a coursemate that output will be more accurate using bigger packet files. :!: Please advise. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: fread problem?

 
0
  #6
Mar 23rd, 2004
Originally Posted by r0ckbaer
  1. for (int i = 0; i < rlen; i++)
  2. {
  3. printf("%x\n", fgetc(input));
  4. }

do it that way
this is wrong. you cannot just call functions without checking the error return of them. fgetc() can fail, and must be checked. and from the ff output u can tell that it is trying to tell u something!! that is EOF or error being returned! lets see the rest of the code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC