Thread: How to move file pointer using gotoxy

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    How to move file pointer using gotoxy

    hi all

    problem is.. i need 2 extract certain data fields from a text file by simply using file pointer. I figurd out gotoxy 2 b a way of doin it ..but how do i move the fp and copy the specific text into a local var..

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    So you want to jump to a certain point in a file? I think fseek() is what you're looking for ... http://www.cplusplus.com/reference/c...dio/fseek.html But that is a 1D file pointer setter. Doesn't work in X and Y. If every line in the file was the same length you could flatten an X and Y coordinate to a single offset.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    ok thanks 4 fseek but dat wud move in one line (row)..as u said. how do i move 2 d next line...and how do i copy d text at d fie pointer location to a local var

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    OK. What you've gotta realise is that carrage returns, i.e. '\n' is also a character. Suppose I have the following file:
    01234
    56789
    with this code:
    Code:
    int main ()
    {
      int i;
      FILE * pFile;
      pFile = fopen ( "myfile.txt" , "r" );
    
      for( i=0; i<10; i++ ) {
        printf( "Position &#37;d has value %d\n", i+1, getc( pFile ) );
      }
    
      fclose( pFile );
    
      return 0;
    }
    You'll notice the 6th position has the decimal value 10. In an ascii graph the 10th element is a new line. It doesn't care about the 2D-ness of the file. It just sees a series of stuff in a line. So if you wanted to move to get the 7, for example, from the file above you'd do something like:
    Code:
    int main ()
    {
      FILE * pFile;
      pFile = fopen ( "myfile.txt" , "r" );
    
      fseek( pFile, 9 , SEEK_SET );
    
      printf( "%c", getc( pFile ) );
    
      fclose( pFile );
    
      return 0;
    }
    fseek( pFile, 9 , SEEK_SET ); The 9 because it starts at 0 and counts the new line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM