Click here to Skip to main content
14,334,458 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have one text file with character 0xA0 in between other characters. I have taken this file contents in string buffer strSrc
I need to remove this character from file.
I had written following code.
But it do not work for 0xA0 (even though it works for other hexadecimal characters).
Pls tell me what is wrong with my code, and what should be done to remove this character from buffer.


    string strRemoveIt= "0xA0";
long iIt = strtol(strRemoveIt.c_str (), NULL, 16);
long lsz = strlen(strSrc.c_str())+1;
char* buff1 = new char[lsz];
char* buff2 = new char[lsz];
strcpy_s(buff1, lsz, strSrc.c_str());
int i,j;
for(i=0,j=0; buff1[i]!='\0'; i++)
{
    if(buff1[i]!=iIt)
    {
        buff2[j]=buff1[i];
        j++;
    }
}
buff2[j]='\0';
ostringstream ost;
ost<<buff2;
strDst = ost.str();
delete [] buff2;
buff2=NULL;
delete [] buff1;
buff1=NULL;
Posted
v2
Comments
Sergey Chepurin 3-Aug-11 6:31am
   
1. Your code doesn't work for any character. Solution 2 works perfect to remove any character, including no-break space (0xA0).
2. To convert from C-string (char*) to basic_ctring (string) use: string basicstring(buff2);
(see http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try
#include <string>
#include <algorithm>

void my_remove(std::string& s, char a)
{ s.erase(std::remove(s.begin(),s.end(),a),s.end()); }


You can call
my_remove(strSrc,'0xA0');


Basically, the std::remove is a standard algorithm that swap everything is equal to a given value to the end of the container, returning the proposed new end for that container.
The erase method will cat away what is between the new and the old end.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

In this statement:
if(buff1[i]!=iIt)

you are comparing a "char" to an "long". Check for "sign extension" in the conversion of 0xA0 (which has the "sign bit" on in the 8 bit byte).

What other hexadecimal values have you tried? Are any of them >= 0x80?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100