1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

[C] filter a buffer, and write to new char

Discussion in 'C' started by ilovefrits, Sep 25, 2009.

  1. ilovefrits

    ilovefrits New Member

    Joined:
    Sep 25, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    Im working on the following code in C, which has to read a char, then filter the symbol - from the char, and write it back without the - symbol to a new char.
    But im currently running into some compile errors, I've been looking around how to work with memset and strcat but still cannot figure out what is going wrong.

    Please help me out :)
    Code:
    
    char neww[255];
    memset(neww, 0, 255);
    for(int i = 0; i<strlen(pid+1); i++)
             { if(pid[i] != "-");
               strcat(neww, pid[i]);
    }
    
    
    where the char pid contains a value like 3434-111-12222-222.
    
    Any suggestions, help ? thank you in advance
     
  2. ilovefrits

    ilovefrits New Member

    Joined:
    Sep 25, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Im sorry, couldn't find the edit button here are the compile errors, and where they occur.

    Error: '!=' : no conversion from 'char *' to 'int'
    Error: '!=' : 'int' differs in levels of indirection from 'char [2]'
    Code:
    { if(pid[i] != "-");
    Error: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
    Code:
      strcat(newpid, pid[i]);

    Thanks in advance, ;)
     
  3. vishnukumar

    vishnukumar New Member

    Joined:
    Sep 24, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Can you post the full code?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    strcat takes two strings, it does not work on a string and a char.
    So strcat(str,"A"); is valid, but strcat(str,'A'); is not, because 'A' is a single char not a string.
    This is why you get the error.
    A better solution is to use two index variables, one that indexes the current character in pid, and one that indexes the current character in neww.
    If the character is one you want, copy it and increment both indices (indices=plural of index).
    If the character is not one you want, just increment the index into pid.
    Don't forget to add a null terminator when you're done.
     
  5. ilovefrits

    ilovefrits New Member

    Joined:
    Sep 25, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thank you for the advice xpi0t0s, I've managed to get the code working.
    I've made a void to process the parsing, because not only one char had to be done but more. Also didn't use the strcat function but managed to do without, also added a null to the char at the end.

    Works fine ;) thank you again for the help/advice. Close ?
     

Share This Page