By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Trim Left char tchar[64]

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You're probably better of using stl strings, and then you have to write
some code as in this link for example
http://www.experts-exchange.com/Prog..._21282559.html

Jul 23 '05 #2

P: n/a
hi,

Thanks for the response

Murugan

Jul 23 '05 #3

P: n/a

<mu******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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::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
Jul 23 '05 #4

P: n/a
Thanks Mike ,It worked like a charm

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.