Results 1 to 5 of 5

Thread: Reseting a file pointer

  1. #1
    ChimpFace9000
    Guest

    Post Reseting a file pointer

    This is in C. After ive read all the way through a file using getc(to get the size of the file), how do i reset the file pointer back to the beginning so i can read the file into a buffer?

  2. #2
    Zaei
    Guest
    Look at "seekf()".

    Z.

  3. #3
    ChimpFace9000
    Guest
    I dont have a manual, so please just tell me how to use it.

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    You actually use "fseek" to set the cursor position in a file. Here is the description of it:

    Code:
    Reposition stream's position indicator.
      Sets the position indicator associated with the stream to a new position defined by an offset from the location given by the origin parameter.
      The End-Of-File indicator of this stream is cleared after a call to fseek. 
      When using fseek on files opened in text mode consider that you can only be sure of the results if an offset of 0 or an offset equal to a value returned from ftell is specified, any other offset value makes result unpredictable because of Carriage-Return translations under this mode. 
      The next operation with the stream after a call to fseek can be either for input or output. 
    
    Parameters. 
    
    stream 
    Pointer to an open file. 
    offset 
    Number of bytes from origin. 
    origin 
    Initial position from where offset is applied. It can be any of these constants defined in stdio.h: SEEK_SET (0) Beginning of file. 
    SEEK_CUR (1) Current position of the file pointer. 
    SEEK_END (2) End of file. 
    
    Return Value.
      If successful the function returns 0. Otherwise it returns nonzero.
    
    
    Portability.
      Defined in ANSI-C. 
    
    Example.
    
    
    /* fseek example */
    #include <stdio.h>
    
    main ()
    {
      FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      fputs ("This is an apple.",pFile);
      fseek (pFile,9,SEEK_SET);
      fputs (" sam",pFile);
      fclose (pFile);
      return 0;
    }
    
    After this code is executed, a file called example.txt will be created and will contain the sentence
    This is a sample.
    Baaaaaaaaah

  5. #5
    jim mcnamara
    Guest
    ansi c -
    PHP Code:
    #include <stdio.h>

    void mainvoid )
    {
       
    FILE *stream;
          
    stream fopen"mfile""r" );
          
    result fseekstream1LSEEK_SET);
          if( 
    result )
             
    perror"Fseek failed" );
          else
             
    printf"File pointer is at the beginning of the file.\n" );
          
    fclosestream );
       }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width