Getting substring on given position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    Getting substring on given position

    I am trying to get the substring or line at a given position.

    #include <stdio.h>

    int main ()
    {
    FILE * pFile;
    pFile = fopen ( "example.tx t" , "w" );
    fputs ( "This is an apple.\nWhat can be done on the apple\n" , pFile );
    fseek ( pFile , 11, SEEK_SET);
    fclose ( pFile );
    return 0;
    }

    using seekg we can point to a particular position but i need substring which ends with newline after the position.
    In the above case
    I need output should be
    apple.\n

    How this can be implemented.Tha nks in advance.

    Manish
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    After reading the line use strstr to find the position and then use strncpy to copy the string from a required position to end of the line.

    Raghu

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      Originally posted by gpraghuram
      After reading the line use strstr to find the position and then use strncpy to copy the string from a required position to end of the line.

      Raghu
      Thank you very much i was looking for getting line which can be done by using getline function. After that i will the method suggested by you.

      Manish

      Comment

      Working...