Thread: string manipulation, the best way?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    string manipulation, the best way?

    i searched the forum but to no avail.

    my problem is that i have a stream of unending data coming through a port. the stream is in chunks terminated by a "\n".

    i want to read it, take away the unchanging parts and print the rest into a file.

    i´ve managed to read the data and was thinking of copying it to another string and then manipulating it. however, i´ve become unstuck here and need some help. i can also write the stream to a file without any problems.

    so far, i have
    Code:
    #include <stdio.h>
    
    //***reads input one character at a time from the input stream called "uncoded"***//
    
    int main( void )
    {
       char uncoded[BUFSIZ];
       int i, ch;
    
       /* Read in single line from "stdin": */
    
       for( i = 0; ((ch = getchar()) != EOF) 
                            && (ch != '\n'); i++ )
       uncoded[i] = (char)ch;
    
       /* Terminate string with null character: */
    
       uncoded[i] = '\0';
    
    
    
    }
    
    ............ 
    
    //
    //**************Writes contents of decoded string to the appropriate file**************/
    	
        fputs ( decoded, fp );
    
    //******************Closes the file and waits for next cycle*************************/
    
        fclose ( fp );
    this will take the data from the stream and copy it to the "uncoded" string. i have to copy it to another string "decoded" and then write it.

    for example, the data coming through the stream is:

    "cabbage 123456789 (random stuff) 1233456789 cabbage"

    i want to isolate the (random stuff) part. another problem is that its not always in the same place so i have to actively look for it.

    any help would be appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Get a line of text from the user:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Then use strncpy() to copy to a new array if you really want to.

    To search through the string, you can:
    - walk it manually (with a loop)
    - use strstr
    - use strchr
    etc etc ... all depending on exactly what you want to do.

    Maybe something like this:
    Code:
    void foo(char in[], char out[])
    {
      /*
       * Magic happens here!
       */  
    }
    
    
    /* somewhere else... */
    
      char encoded[BUFSIZ];
      char decoded[BUFSIZ];
      
      read_a_line(encoded);
      
      foo(encoded, decoded);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    j_spinto
    Guest
    stream sockets do not have to be used with network connections do they?
    i do not understand much, but i think they can be useful

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    hey I'm doing a similar program. Its not perfect yet but its getting better slowly (been quite busy irl) . Anyhoo if you want to see how I have done it, its here .

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Where?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    a big thanks to everyone who has replied. the info has been really useful and i´m currently working on it so i cant say if it works or not.

    i´ll get back to you very soon.

    thank you all once again

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    p.s. is there anyway of deleting the data in a string??

    for example like a reverse
    Code:
    strcat
    then i can have a string with the unchanging stuff and take it away from the incoming string to leave me the changed (random) stuff?

  8. #8
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Quote Originally Posted by shoobsie
    p.s. is there anyway of deleting the data in a string??

    for example like a reverse
    Code:
    strcat
    then i can have a string with the unchanging stuff and take it away from the incoming string to leave me the changed (random) stuff?

    one way to do it would be to create a new string, copy everything you want to keep into the new string, and then either get rid of the old string, or replace the old one with the new one.

    String editing is difficult because, remember, the way it is stored in memory is usually like this:
    Code:
    [H][e][l][l][o][ ][w][o][r][l][d][!][\0]
    so, if you weant to get rid of 'world' and keep hello, it's easy, all you have to do is insert a null character (string terminator) in position 5 (the space), but if you want to get rid of "Hello " then probably the easiest way is to make a new string.

    you could also make your own struncat function
    Code:
    #include <stdio.h>
    #include <string.h>
    
    //return values: -1 for a failure, 0 or a positive number for the
    // number of times it removed 'remove' from 'string'
    int struncat(char* string, char* remove)
    {
      if(!string || !remove) return -1;
      char* temp=0;
      temp = (char*)malloc(strlen(string));
      //make sure it was allocaed
      if(!temp) return -1;
      char* s = string;
      char* t = temp;
      int x=0;
      while(*s != '\0') {
        if(!strncmp(s, remove, strlen(remove))) {
          s += strlen(remove);
          x++;
        } else {
          *t = *s;
          t++;
          s++;
        }
      }
      *(t++) = '\0';
      strcpy(string, temp);
      return x;
    }
    
    int main(int argc, char* argv[])
    {
      char string[1024];
      char remove[32];
      strcpy(string, "Hello, World, Hello!");
      strcpy(remove, "Hello");
      if(struncat(string, remove)>=0) printf("struncat was successful.\n");
      printf("%s\n", string);
      return 0;
    }
    Code:
    output:
    struncat was successful.
    , World, !
    something like that.. not sure if that actually works for you, but check it out.
    Last edited by Cobras2; 06-29-2005 at 12:41 PM. Reason: formatting troubles
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    excellent post cobras!!

    i actually managed to use
    Code:
    strtok
    to remove some of the data but i forgot to mention that each "section" is divided by a " , " and so it makes life easier. i´ll try this and get back to you.

    thank you very much though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM