resetting a file pointer 
Author Message
 resetting a file pointer

Hi guys , wonder if someone could help me.

if ((my_ptr = fopen("myfile", "r" )) != NULL){

                // Count how many lines
                line_ctr=0;
                while(1){
                        ch=fgetc(my_ptr);
                        if ( ch == EOF )
                                break;
                        if ( ch == '\n' )
                                line_ctr++;

                }

The above code reads through the file. My question is how do I reset it to
the to the beginning in essence to re- read the file again, instead of
closing it and re-opening it to get it to the beginning.



Fri, 11 Feb 2005 19:43:07 GMT  
 resetting a file pointer

Quote:

> Hi guys , wonder if someone could help me.

> if ((my_ptr = fopen("myfile", "r" )) != NULL){

>                // Count how many lines
>                line_ctr=0;
>                while(1){
>                        ch=fgetc(my_ptr);
>                        if ( ch == EOF )
>                                break;
>                        if ( ch == '\n' )
>                                line_ctr++;

>                }

> The above code reads through the file. My question is how do I reset it to
> the to the beginning in essence to re- read the file again, instead of
> closing it and re-opening it to get it to the beginning.

rewind(my_ptr);

        - Kevin.



Fri, 11 Feb 2005 19:52:25 GMT  
 resetting a file pointer

Quote:

> Hi guys , wonder if someone could help me.

> if ((my_ptr = fopen("myfile", "r" )) != NULL){

>                 // Count how many lines
>                 line_ctr=0;
>                 while(1){
>                         ch=fgetc(my_ptr);
>                         if ( ch == EOF )
>                                 break;
>                         if ( ch == '\n' )
>                                 line_ctr++;

>                 }

> The above code reads through the file. My question is how do I reset it to
> the to the beginning in essence to re- read the file again, instead of
> closing it and re-opening it to get it to the beginning.

rewind(my_ptr);

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 11 Feb 2005 20:16:29 GMT  
 resetting a file pointer
On Mon, 26 Aug 2002 21:43:07 +1000, in comp.lang.c , "Tommy"

Quote:

>The above code reads through the file. My question is how do I reset it to
>the to the beginning in essence to re- read the file again, instead of
>closing it and re-opening it to get it to the beginning.

fseek

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sat, 12 Feb 2005 07:23:43 GMT  
 resetting a file pointer

Quote:

> >The above code reads through the file. My question is how do I reset it
to
> >the to the beginning in essence to re- read the file again, instead of
> >closing it and re-opening it to get it to the beginning.

> fseek

fseek() doesn't reset the flags. rewind() is designed for this problem.


Sun, 13 Feb 2005 01:52:01 GMT  
 resetting a file pointer
On Tue, 27 Aug 2002 18:52:01 +0100, in comp.lang.c , "Malcolm"

Quote:


>> >The above code reads through the file. My question is how do I reset it
>to
>> >the to the beginning in essence to re- read the file again, instead of
>> >closing it and re-opening it to get it to the beginning.

>> fseek

>fseek() doesn't reset the flags. rewind() is designed for this problem.

fseek(foo, 0, SEEK_SET) will move the file pointer to the start. I
agree that rewind also resets the error flag and eof flags, if any. On
the other hand, fseek returns 0 if it worked, rewind does not inform
you, and may silently fail. YMMV but I prefer one that tells me whats
happened.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sun, 13 Feb 2005 06:30:36 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. AddIn-CommandBar-Reset()-How to stop user resetting toolbar

2. How to reset pointer

3. Can not reset an open ifstream file.

4. file pointer at end of file?

5. Q: FILE Data Structure / File Position Pointer?

6. file name from file pointer?

7. passing a FILE pointer to a FILE variable

8. Help on FILE pointers and file access

9. How to get file descriptor from file pointer?

10. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

11. ERROR IN READING FILE : File Pointer

12. difference b/w file pointer and file descriptor

 

 
Powered by phpBB® Forum Software