simple string editing

This is a discussion on simple string editing within the C Programming forums, part of the General Programming Boards category; I would like to know how to print characters after ')' has been read... I need this to extract the ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    21

    simple string editing

    I would like to know how to print characters after ')' has been read... I need this to extract the data from a file that contains 100's of .pls files which are used for connecting to web streams, here's what 1 .pls file looks like...

    [playlist]
    numberofentries=3
    File1=http://64.236.34.97:80/stream/1065
    Title1=(#1 - 871/18508) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length1=-1
    File2=http://64.236.34.67:80/stream/1065
    Title2=(#2 - 719/14952) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length2=-1
    File3=http://64.236.34.4:80/stream/1065
    Title3=(#3 - 842/16389) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length3=-1
    Version=2

    What i want to get is everything after the Title1=(#1 - 871/18508)

    At first I had thought of using strncpy to do this. but if the number of characters in parenthesis changes, that causes a problem.

    I also would like to take the address from this by grabbing everything after File1=
    I am assuming this is exactly the same as taking everything after ')'

    Thank you very much

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can use strchr() to find a specific character within a string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    21
    I am having a hard time understanding how to get the output i want.

    I want to say this...

    Code:
    temp_var = strchr(string, "=");
    however what do I declare temp_var as? and how would I print the output. strchr returns a pointer. that is what is confuseing me.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Another possible approach...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          int ch;
          while ( (ch = getc(file)) != EOF )
          {
             if ( ch == ')' )
             {
                do {
                   ch = getc(file);
                   if ( ch == EOF )
                   {
                      goto done;
                   }
                   putchar(ch);
                } while ( ch != '\n' );
             }
          }
       done:
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    [playlist]
    numberofentries=3
    File1=http://64.236.34.97:80/stream/1065
    Title1=(#1 - 871/18508) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length1=-1
    File2=http://64.236.34.67:80/stream/1065
    Title2=(#2 - 719/14952) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length2=-1
    File3=http://64.236.34.4:80/stream/1065
    Title3=(#3 - 842/16389) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    Length3=-1
    Version=2
    */
    
    /* my output
     D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
     D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
     D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
    */
    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
    Mar 2005
    Posts
    37
    Code:
    while ( (ch = getc(file)) != EOF )
          {
             if ( ch == ')' )
             {
                do {
                   ch = getc(file);
                   if ( ch == EOF )
                   {
                      goto done;
                   }
                   putchar(ch);
                } while ( ch != '\n' );
             }
          }
    I guess it would not be a good idea to use goto.. instead u can use a break

    Yes you will have to adjust the getc(file)..

    instead of using it in the while loop you can have it as a statement before it ..
    and in the while loop you can just compare (ch!=EOF)
    The code given by dave is gr8.. i'm just giving another way of solving the problem..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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