1

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;
   }
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
James
  • 45
  • 5

1 Answers1

1

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;
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490