Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question about change of "fp" in function "fseek"and "ftell" (http://www.velocityreviews.com/forums/t444104-question-about-change-of-fp-in-function-fseek-and-ftell.html)

Chen ShuSheng 08-24-2006 09:36 AM

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;
}
-------------





Ancient_Hacker 08-24-2006 10:40 AM

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

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.


Chen ShuSheng 08-24-2006 11:07 AM

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

"Ancient_Hacker" <grg2@comcast.net>
??????:1156416048.583197.77060@75g2000cwc.googlegr oups.com...
>
>
>
>> 2. If fp is not change after "fseek", why last is not
>> ZORE?

>
> What's "ZORE" ?
>


Sorry, I mean "zero".




Ancient_Hacker 08-24-2006 12:57 PM

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

Chen ShuSheng wrote:
> "Ancient_Hacker" <grg2@comcast.net>
> ??????:1156416048.583197.77060@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.



All times are GMT. The time now is 11:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.