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::size_type pos(s.find_first_not_of(" "));
if(pos != std::string::npos)
{
s.erase(0, pos);
s += '\0';
std::copy(s.begin(), 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