Reputation: 45
I have a C++ snippet that removes the first word of a line in a text file e.g.
test C:\Windows\System32
download C:\Program Files\test.exe
Although it removes the first word, there is a space left over after trimming it, is there a way to stop remove this space?
#include <iostream>
using namespace std;
int main()
{
string tmp;
while ( !cin.eof() )
{
cin >> tmp;
getline(cin, tmp);
cout << tmp << endl;
}
}
Upvotes: 1
Views: 1336
Reputation: 612993
You just need to trim tmp
before streaming it to cout
. Use your favourite trim
function from a library, or write your own.
You can find some options for trim functions here: What's the best way to trim std::string?
Once you have a trim function, and Evan Teran's versions look rather fine, you then can write:
cin >> tmp;
getline(cin, tmp);
cout << trim(tmp) << endl;
Upvotes: 1