473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

fseek()

I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my sender
app, the sender and the receiver are both on the same machine and I
have an Internet emulation gateway running on the same machine as
well.

What I'm confused about is I can't detect when the end of the file I'm
sending has been reached, my program seems to be running forever.

Bascially the window[] in my code fragment is generated by the
receiver app, it contains the sequence numbers of packets, a zero
value in window indicates that packet has been successfully received
by the receiver, non-zero values indicate the sequence numbers of
packets that were not received by the receiver, thus need to be resent
by the sender app.

So I need to use fseek() to seek through the file, using the values in
window[] as offsets, but my receiver is stupid it doesn't know the
biggest possible sequence number, it just asks for greater sequence
numbers if previous smaller sequence numbers have all been received,
so in window[] it might contain some sequence number that when used as
offset to fseek(), fseek() will seek beyond the actual end of the
stream representing the file(I maybe wrong here becasue of my poor
understanding of stream, file etc..), I tried to find the size of the
file first then try to stop invoke fseek() , if offset times packet
size is greater than my file_size, but that didn't stop the pain, it
still runs forever.

Thank you all for any help you could provide.

Eric
..........
...........
while(last_packet_acked == 0){

for(i=0;i<win_size;i++){
if(window[i]==0){continue;}

offset = window[i]-1;
printf("size:%u\n",offset*5);

if(offset*5 <= file_size){fseek(file,offset,SEEK_SET);}
if((bytes_read=fread(packet.data,1,sizeof(packet.d ata),file))!=0)
{
packet.client_seq_no = htonl(window[i]);
n = sendto(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
*)&gateway,sizeof(struct sockaddr_in));
if(n<0) error("Error sendto");
printf("SENT PACKET:%u\n",window[i]);

}else{
packet.client_seq_no = htonl(window[i]);
packet.flag = htons(FIN);
n = sendto(sock,&packet,sizeof(struct dgram),0,(struct
sockaddr *)&gateway,sizeof(struct sockaddr_in));
if(n<0) error("Error sendto");
printf("SENT PACKET:%u\n",window[i]);
break;
}

}
...............
...............

Mar 23 '07 #1
6 3713
On 23 Mar 2007 16:27:13 -0700, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my sender
For the sockets part I suggest you ask again in comp.unix.programming,
where sockets are topical.
>What I'm confused about is I can't detect when the end of the file I'm
sending has been reached, my program seems to be running forever.
while(last_packet_acked == 0){
The outer while loop runs as long as last_packet_acked is zero, where
do you set it to nonzero?
if(offset*5 <= file_size){fseek(file,offset,SEEK_SET);}
how did you determine file_size? Also, whats that brace doing at the
end of the line?
if((bytes_read=fread(packet.data,1,sizeof(packet.d ata),file))!=0)
This will get executed irrespective of whether offset*5 file_size.
Is that your intention.

Frankly I found your code extremely difficult to read as the brace
style was confusing and your newsreader stipped much of the
indentation.

Break the programme down into smaller segments. Concentrate on walking
through the file first, forget about the sockets part till you
understand how to manipulate the stream of data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 23 '07 #2
On 23 Mar, 23:51, Mark McIntyre <markmcint...@spamcop.netwrote:
On 23 Mar 2007 16:27:13 -0700, in comp.lang.c , "ericunfuk"

<xuwenduan2...@gmail.comwrote:
I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.
I'm trying to send a file using UDP, below is a segment of my sender

For the sockets part I suggest you ask again in comp.unix.programming,
where sockets are topical.
What I'm confused about is I can't detect when the end of the file I'm
sending has been reached, my program seems to be running forever.
while(last_packet_acked == 0){

The outer while loop runs as long as last_packet_acked is zero, where
do you set it to nonzero?
if(offset*5 <= file_size){fseek(file,offset,SEEK_SET);}

how did you determine file_size? Also, whats that brace doing at the
end of the line?
if((bytes_read=fread(packet.data,1,sizeof(packet.d ata),file))!=0)

This will get executed irrespective of whether offset*5 file_size.
Is that your intention.

Frankly I found your code extremely difficult to read as the brace
style was confusing and your newsreader stipped much of the
indentation.

Break the programme down into smaller segments. Concentrate on walking
through the file first, forget about the sockets part till you
understand how to manipulate the stream of data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?

Mar 24 '07 #3
In article <11*********************@e1g2000hsg.googlegroups.c om>,
ericunfuk <xu***********@gmail.comwrote:
>If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?
fseek() always clears the end-of-file indicator (unless it fails).

Imagine you've just read the end-of-file, and you seek back to the
middle. You don't want feof(file) to still be true, because a read
would now succeed.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 24 '07 #4
ericunfuk wrote:
>
I have posted a few other posts before this one today, I still
can't solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my
sender app, the sender and the receiver are both on the same
machine and I have an Internet emulation gateway running on the
same machine as well.
I believe you have already been told that this is not topical
here. If you want to avoid plonk files and be able to get future
help on topical C questions, post this stuff elsewhere.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 24 '07 #5
In article <11**********************@y80g2000hsf.googlegroups .com>
ericunfuk <xu***********@gmail.comwrote:
>I'm trying to send a file using UDP ...
If you are really using UDP, you are probably using sockets (which
lie outside the boundaries of Standard C, so you have to do something
"non-standard" to use them -- which means everything the Standard
tells you goes right out the window, at least potentially).

If you *are* using sockets, beware: fseek() does not work on sockets.
(This is one of the things that you must give up in order to use
networking.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 24 '07 #6
On 23 Mar 2007 17:28:45 -0700, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>
If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?
The question indicates that you /still/ haven't understood the
difference between the end of a file, and the EOF flag in the
stream.The EOF flag is held by the little guy at the finishing line of
the race. He isn't the end of the race himself, he just tells you when
you got there.

As for your question: Imagine you're looking at a bookshelf. You can
move your finger to and fro over the shelf, and even right off the end
of the shelf into empty space. You're fseeking() to and fro, which is
allowed, so no alert flags get raised.

Now try to pick up a book to fread() it. You can only do that if
you're pointing inside the bookcase. If you're pointing outside the
bookcase, the EOF flag is waved to indicate you're trying to take a
book out of thin air.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 24 '07 #7

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

Similar topics

62
by: Christopher Benson-Manica | last post by:
On thinking about the "replace a word in a file" thread, I wondered how easy it would be to accomplish the same thing with only one file pointer. This led me to some questions... "For a text...
15
by: TJ Walls | last post by:
Hello All, I am baffled ... I am trying to improve the speed of a program that I have written that performs random access within a file. It relies heavily on fseek and is very slow. To test, I...
10
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list...
2
by: cedarson | last post by:
I am writing a program and have been instructeed to use the 'fseek', 'ftell', and 'stat' functions, however, after looking in the online manual for each of these, I am still unsure on how to use...
10
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows,...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
6
by: ericunfuk | last post by:
A basic question: When the documentation says "fseek() clears EOF indecator, does it mean when you seek over EOF, EOF is no longer at the original position and hence removed?Say, after I seek...
20
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...
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.