Copying part of a textline

This is a discussion on Copying part of a textline within the C Programming forums, part of the General Programming Boards category; Hi, I have been struggling with this problem for some hours now. I want to copy a certain part of ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    Copying part of a textline

    Hi,

    I have been struggling with this problem for some hours now.
    I want to copy a certain part of a textline.

    I have tried a few approches and at the moment I am using strtok to split the text up but I don't know how to get the text after splitting it.

    This is the line I'm working on:

    <td align="left" valign="top"class="valuetxt" style="cursor:pointer;color:#0066FF;" onClick="javascript:rowedit('80440');">&nbsp;1&nbsp;</td>

    (the bold part is what I'm after)

    and this is my most successful code so far:

    Code:
        char sep[6]= "&nbsp;";
        char *result = NULL;
    
                    result = strtok(c, sep);
                    while(result != NULL){
                            printf("%s\n", result);
                            result = strtok(NULL, sep);
    c is where the textline is located and read from.

    Here I am able with the help of strtok to list up all of the splits from the textline and the second last is the one I want.

    Is this a bad approch?
    Any hint/tip is appreciated.


    ----------------------------------------------------------------

    I was able to figure out a solution:

    Code:
                    result = strtok(c, sep);
                    while(result != NULL){
                            //printf("%s\n", result);
                            result = strtok(NULL, sep);
                            if (strstr(result, "\">")!= NULL){
                                result = strtok(NULL, sep);
                                printf("%s \n", result);
                                break;
                            }    
                    }
    Last edited by tboy; 11-25-2004 at 08:17 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char sep[6]= "&nbsp;";
    It's not a good idea to put 7 characters in a 6-character array. Using this would be better.
    Code:
    char sep[]= "&nbsp;";
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Ok thanks for pointing that out.

    One more, can anyone tell me how to I can get my code to repeat itself?

    Code:
    if (fgets(c, sizeof(c), file)!= NULL && strstr(c, "p;")!= NULL){
                    
                    result = strtok(c, sep);
                    while(result != NULL){
                            //printf("%s\n", result);
                            result = strtok(NULL, sep);
                            if (strstr(result, "\">")!= NULL){
                                result = strtok(NULL, sep);
                                fprintf(nfile, "%s ", result);
                                printf("%s \n", result);
                                break;
                            }    
                    }                                
                   
                    //printf(c);
                }
    That code hast to be repeated 7 times.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    while (fgets(c, sizeof(c), file)!= NULL && strstr(c, "p;")!= NULL){
    Maybe add a counter to break on 7.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    I tried this but I doesn't seem to repeat

    Code:
    while (fgets(c, sizeof(c), file)!= NULL && strstr(c, "p;")!= NULL){
                    if (d <= 7){
                        d++; 
                    }
                    else{
                       break;
                    }    
                    result = strtok(c, sep);
                    while(result != NULL){
                            //printf("%s\n", result);
                            result = strtok(NULL, sep);
                            if (strstr(result, "\">")!= NULL){
                                result = strtok(NULL, sep);
                                fprintf(nfile, "%s ", result);
                                printf("%s \n", result);
                                break;
                            }    
                    }                                
                    //printf(c);
                }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Yeah, shoot. I noticed the && strstr(c, "p;")!= NULL part after I posted. Consider moving it into the loop. You are intending to read a file line by line and parse each line for this, right?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Yes that is what I am trying to do.

    I will try to move that into the loop.

    ------------------------
    Edit
    ------------------------

    it works now, thanks.

    but it seems to crash on this line:

    PHP Code:
    <td align="left" valign="top" class="valuetxt" style="cursor:pointer;color:#0066FF;" onClick="javascript:rowedit('80440');">&nbsp;Nokia 6220 Tri-Band Vibra MMS<br
    I guess its to long but how can I prevent an overflow.
    Last edited by tboy; 11-25-2004 at 09:51 AM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Say, you're just after the number between &nbsp;'s, right? Could you do something like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char line [ 256 ];
          while ( fgets(line, sizeof line, file) )
          {
             char *found = strstr(line, "&nbsp;");
             if ( found )
             {
                int number;
                if ( sscanf(found, "&nbsp;%d&nbsp;", &number) == 1 )
                {
                   printf("number = %d\n", number);
                }
             }
          }
          fclose(file);
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 11-25-2004 at 10:01 AM. Reason: Twiddles and tweaks.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Well I am after what is between the two &nbsp; so your code
    should work with other characters like letters?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It could be bent that way. But I think your strstr approach might be better.

    [edit]Something like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char line [ 256 ];
          while ( fgets(line, sizeof line, file) )
          {
             const char delimiter[] = "&nbsp;";
             char *found = line;
             while ( found = strstr(found, delimiter) )
             {
                char *text = found + strlen(delimiter);
                char *end  = strstr(text, delimiter);
                if ( !end )
                {
                   break;
                }
                *end = '\0';
                printf("text = \"%s\"\n", text);
                found = end + strlen(delimiter);
             }
          }
          fclose(file);
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 11-25-2004 at 03:21 PM. Reason: [1] Added code. [2] Tweaked and twiddled. [3] Found crayons. [4] Aw, heck -- why not. Added loop for multiple matches within the same line.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Thanks, your approach was far superior than mine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  2. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  3. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  4. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21