<mu******@gmail .com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I read a text file into a char variable
char tchar[64];
I want to trim leading spaces (Left trim).How can I do that in C++.
Any input will be appreciated.
#include <algorithm>
#include <iostream>
#include <string>
void trimleft(char *arr)
{
std::string s(arr);
std::string::si ze_type pos(s.find_firs t_not_of(" "));
if(pos != std::string::np os)
{
s.erase(0, pos);
s += '\0';
std::copy(s.beg in(), s.end(), arr);
}
else
*arr = 0;
}
void show(char *arr, const char *prefix = "")
{
std::cout << prefix << '[' << arr << ']' << '\n';
}
int main()
{
char tchar[64] = " Hello world ";
show(tchar, "Before:\n" );
std::cout.put(' \n');
trimleft(tchar) ;
show(tchar, "After:\n") ;
return 0;
}
-Mike