Thread: how to read string from half way through a file?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    how to read string from half way through a file?

    hi all

    straight to the point, i have set my string breakers e.g.: filenameRfile contentsQ
    the R and the Q are my point string breakers

    i have got the filename part of my program to create the file under that file name but now i am struggling to copy the file's contents because i do not know how to get it to read only between Q & R

    Code:
    char *pos2 = strchr(heading, 'Q');
    int pos2a = pos2-heading;
    i have used this to find out how many characters there are til Q is reached

    this then doesnt carry on from R it starts all over again

    is there a way to start it from a certain spot?
    kind regards

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    anybody shed some light on this?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What exactly are you trying to do... Can you show us some source code?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    R_p = strchr(heading,'R');
    Q_p = strchr(heading,'Q');
    // check for NULL
    n = Q_p - R_p;

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by Bayint Naung View Post
    Code:
    R_p = strchr(heading,'R');
    Q_p = strchr(heading,'Q');
    // check for NULL
    n = Q_p - R_p;
    what do you actually mean by check for null?

    i mean after the file name there is no null its just: filenameRfile contents\0Q

    i need to find R and minus the first part out of my buffer

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What he's saying is that if R or Q do not exist, strchr will return null.

    Also, if R_p is above, then R_p+1 is your string that you so desperately want.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok well ive tried this now and i still cannot get anywhere because i cant seem to be able to take the text away from the start

    Code:
    	char *pos = strchr(heading, 'R');
    	char *pos2 = strchr(heading, 'Q');
    	int posa = pos-heading;
    	int pos2a = pos2-heading;
    	char pos2b = pos2a - posa;
    When i use this pos2b shows as 18 characters
    this is only because pos2a which is 23 is being mised posa which is (correctly) 5

    i have had a look through strncpy but no luck either from that side
    Last edited by aadil7; 01-10-2011 at 05:15 PM.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Code:
          strcpy (bufferb, &heading[pos2a]);
          bufferb[pos2a]='\0';
          printf("bufferb is %s\n", bufferb);
    THIS IS WHAT I HAVE COME UP WITH BUT MY ONLY ISSUE IS THAT I GET A SEGMENTATION FAULT WITH IT
    Last edited by aadil7; 01-10-2011 at 07:07 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you care about keeping the original string intact?
    Code:
    char *pos = strchr(heading, 'R');
    pos++;
    char *pos2 = strchr(heading, 'Q');
    *pos2 = 0;
    If you need to keep the string intact, then don't do the last line. pos is still your string.
    pos is exactly what you want.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You guys sure like over-complicating things.
    Code:
    char buf[BUFSIZ]={0};
    
    strncpy( buf, R, Q - R );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM