Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > remove junk charecters

Reply
Thread Tools

remove junk charecters

 
 
Index
Guest
Posts: n/a
 
      10-06-2006
Hi,
I am trying to compare a char* with an unsigned char*.I have type cast
the later to char*.Now the problem is, the unsigned char* is populated
with recv() function over the socket and sometimes it containes junk.I
need to compare the received message over the socket with a particular
charecter 'r' to terminate the entire process.
Any help will be highly appreciated.The code snippet and my output is
as follows:

if(!(strcmp(message,"q"))){
Close(clntSocket);
exit(0);
}

output screen:

The received message is :q©

Length of the received message 1


** Though I typed q, the received message has a junk charecter at its
end and hence the comparison dosn't work.but sometimes after two/three
failed times, the string is without any junk charecter.

Thanks.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-06-2006
Index wrote:
> I am trying to compare a char* with an unsigned char*.I have type cast
> the later to char*.Now the problem is, the unsigned char* is populated
> with recv() function over the socket and sometimes it containes junk.I
> need to compare the received message over the socket with a particular
> charecter 'r' to terminate the entire process.
> Any help will be highly appreciated.The code snippet and my output is
> as follows:
>
> if(!(strcmp(message,"q"))){
> Close(clntSocket);
> exit(0);
> }
>
> output screen:
>
> The received message is :q©
>
> Length of the received message 1
> q©
>
> ** Though I typed q, the received message has a junk charecter at its
> end and hence the comparison dosn't work.but sometimes after two/three
> failed times, the string is without any junk charecter.


You need to use 'strncmp' or simply compare the first character of
the message:

if (message[0]=='q') {
Close(clntSocket);
exit(0);
}

Note the single quotes around the q.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Index
Guest
Posts: n/a
 
      10-06-2006
but if the message starts with q but has more characters after it?If I
compare only teh first charecter, it will not be appropriate.
Victor Bazarov wrote:
> Index wrote:
> > I am trying to compare a char* with an unsigned char*.I have type cast
> > the later to char*.Now the problem is, the unsigned char* is populated
> > with recv() function over the socket and sometimes it containes junk.I
> > need to compare the received message over the socket with a particular
> > charecter 'r' to terminate the entire process.
> > Any help will be highly appreciated.The code snippet and my output is
> > as follows:
> >
> > if(!(strcmp(message,"q"))){
> > Close(clntSocket);
> > exit(0);
> > }
> >
> > output screen:
> >
> > The received message is :q©
> >
> > Length of the received message 1
> > q©
> >
> > ** Though I typed q, the received message has a junk charecter at its
> > end and hence the comparison dosn't work.but sometimes after two/three
> > failed times, the string is without any junk charecter.

>
> You need to use 'strncmp' or simply compare the first character of
> the message:
>
> if (message[0]=='q') {
> Close(clntSocket);
> exit(0);
> }
>
> Note the single quotes around the q.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      10-06-2006
> Victor Bazarov wrote:
>> Index wrote:

[receiving data with recv, trying to compare with c-string]
>>> ** Though I typed q, the received message has a junk charecter at its
>>> end and hence the comparison dosn't work.but sometimes after two/three
>>> failed times, the string is without any junk charecter.

>> You need to use 'strncmp' or simply compare the first character of
>> the message:
>>
>> if (message[0]=='q') {
>> Close(clntSocket);
>> exit(0);
>> }
>>
>> Note the single quotes around the q.


Index wrote:
> but if the message starts with q but has more characters after it?If I
> compare only teh first charecter, it will not be appropriate.


Please don't top-post in this group. See the link in my signature.

The problem is, that you don't send/receive null-terminated strings. Either
null-terminate the received buffer before comparing, or compare with
strncmp and give as length parameter the number of received bytes.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
 
Reply With Quote
 
Index
Guest
Posts: n/a
 
      10-06-2006
Thanks.Its fixed.
Thomas J. Gritzan wrote:
> > Victor Bazarov wrote:
> >> Index wrote:

> [receiving data with recv, trying to compare with c-string]
> >>> ** Though I typed q, the received message has a junk charecter at its
> >>> end and hence the comparison dosn't work.but sometimes after two/three
> >>> failed times, the string is without any junk charecter.
> >> You need to use 'strncmp' or simply compare the first character of
> >> the message:
> >>
> >> if (message[0]=='q') {
> >> Close(clntSocket);
> >> exit(0);
> >> }
> >>
> >> Note the single quotes around the q.

>
> Index wrote:
> > but if the message starts with q but has more characters after it?If I
> > compare only teh first charecter, it will not be appropriate.

>
> Please don't top-post in this group. See the link in my signature.
>
> The problem is, that you don't send/receive null-terminated strings. Either
> null-terminate the received buffer before comparing, or compare with
> strncmp and give as length parameter the number of received bytes.
>
> --
> Thomas
> http://www.netmeister.org/news/learn2quote.html


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Garbage Charecters Vishal Dalsania ASP .Net 0 07-27-2006 02:59 PM
I want to exclude an expression of a sequence of charecters with regex 28tommy Python 2 12-28-2005 12:45 PM
I want to read all charecters of pdf file ... Alex Smith ASP .Net 1 06-17-2005 01:25 PM
What charecters the lightweight component have? Bruce Sam Java 1 12-17-2004 08:22 AM
performing action on set of charecters jeff Python 2 01-23-2004 07:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57