473,396 Members | 1,816 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,396 software developers and data experts.

Question about change of "fp" in function "fseek"and "ftell"

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); /*add by me*/
last = ftell(fp);
cout<<"last="<<last<<"\t"; /*add by me*/
-------------------------
My question is: 1. why fp do not change after function "fseek"?
2. If fp is not change after "fseek", why last is not ZORE?
3. How does "fseek" return Current position?
It will be much preciated if you can help me. Thanks.

Below is the whole code:
----------------
/* reverse.c -- displays a file in reverse order */
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define CNTL_Z '\032' /* eof marker in DOS text files */
#define SLEN 50
int main(void)
{
char file[SLEN];
char ch;
FILE *fp;
long count, last;
int test;

puts("Enter the name of the file to be processed:");
gets(file);
if ((fp = fopen(file,"rb")) == NULL)
{ /* read-only and binary modes */
printf("reverse can't open %s\n", file);
exit(1);
}

cout<<fp<<"\t";
fseek(fp, 0L, SEEK_END); /* go to end of file */
cout<<fp<<"\t";
last = ftell(fp);
cout<<"last="<<last<<"\t";
/* if SEEK_END not supported, use this instead */
/* last = 0;
while (getc(fp) != EOF)
last++;
*/
for (count = last- 1; count >= 0; count--)
{
test=fseek(fp, count, SEEK_SET); /* go backward */
ch = getc(fp);
/* for DOS, works with UNIX */
if (ch != CNTL_Z && ch != '\r')
putchar(ch);
/* for Macintosh */
/* if (ch == '\r')
putchar('\n');
else
putchar(ch)
*/
}
putchar('\n');
fclose(fp);

return 0;
}
-------------


Aug 24 '06 #1
3 2913

Chen ShuSheng wrote:
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); /*add by me*/
last = ftell(fp);
cout<<"last="<<last<<"\t"; /*add by me*/
-------------------------
My question is: 1. why fp do not change after function "fseek"?

fp is a pointer to a FILE structure. Which sounds a lot like "file
pointer", but it's not.
fp is the address in memory where the file info for the file is kept.
It will not change.

2. If fp is not change after "fseek", why last is not ZORE?
What's "ZORE" ?

3. How does "fseek" return Current position?
it does not, it returns success or failure. You have to call ftell to
get the position.

Aug 24 '06 #2

"Ancient_Hacker" <gr**@comcast.net>
??????:11*********************@75g2000cwc.googlegr oups.com...
>

> 2. If fp is not change after "fseek", why last is not
ZORE?

What's "ZORE" ?
Sorry, I mean "zero".

Aug 24 '06 #3

Chen ShuSheng wrote:
"Ancient_Hacker" <gr**@comcast.net>
??????:11*********************@75g2000cwc.googlegr oups.com...


2. If fp is not change after "fseek", why last is not
ZORE?
What's "ZORE" ?

Sorry, I mean "zero".
oh! I shudda guessed that. ftell() is telling you the file
position.

Aug 24 '06 #4

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

Similar topics

9
by: hstagni | last post by:
I tried to use fseek in a file opened for writing: ------ BEGIN ------- int main() { char c; FILE *fp; fp=fopen("texto", "wb"); putc('a', fp); putc('b', fp);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.