0

Not really asking about parsing command line arguement but rather the code (C++) to read them in

I have wrote in MSVC2010. Select Project\DEbugging\Command Line Arguement), I have written the following

"testvid" 10000 15000

all separated by space

FOr my read in code, I have used string stream to read them in

string folder;
int begin;
int end;

if(argc = 4){
    std::stringstream ssArgConverter;

    folder = argv[1];

    ssArgConverter << argv[2];
    if(ssArgConverter >> begin)
    {
       // success
       ssArgConverter.str("");
    }

    ssArgConverter << argv[3];
    if(ssArgConverter >> end)
    {
       // success
       ssArgConverter.str("");
    }

}

HAve tested the program but I seem to have problem reading in the last argument. THe printed value for the argument in my program is

folder : testvid
begin  : 10000
end    : -89456273

Just cant figure out what is wrong. Need some help here. Thanks

user1538798
  • 1,075
  • 3
  • 17
  • 42

1 Answers1

1

You need to clear your stringstream, not set its content to "".

string folder;
int begin;
int end;

if(argc == 4){
    std::stringstream ssArgConverter;

    folder = argv[1];

    ssArgConverter << argv[2];
    if(ssArgConverter >> begin)
    {
       // success
       ssArgConverter.str("");
       ssArgConverter.clear();
    }

    ssArgConverter << argv[3];
    if(ssArgConverter >> end)
    {
       // success
       ssArgConverter.str("");
       ssArgConverter.clear();
    }
}

EDIT: this question is pretty similar to: How to clear stringstream? and the accepted answer has a pretty good explanation.

So you actually should use .str("") or .str(std::string()) and .clear()

Community
  • 1
  • 1
Michael B.
  • 331
  • 1
  • 5
  • Thanks for the help! Didnt know that would need to have an additional .clear(). Have been just using the .str("") method whenever I am parsing data in a file and it has always work for me – user1538798 Jan 15 '15 at 02:23