Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   [C] filter a buffer, and write to new char (http://www.go4expert.com/forums/c-filter-buffer-write-char-t19554/)

ilovefrits 25Sep2009 21:10

[C] filter a buffer, and write to new char
 
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

ilovefrits 25Sep2009 21:28

Re: [C] filter a buffer, and write to new char
 
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, ;)

vishnukumar 26Sep2009 05:20

Re: [C] filter a buffer, and write to new char
 
Can you post the full code?

xpi0t0s 26Sep2009 12:33

Re: [C] filter a buffer, and write to new char
 
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.

ilovefrits 26Sep2009 22:11

Re: [C] filter a buffer, and write to new char
 
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 ?


All times are GMT +5.5. The time now is 17:49.