c++ - String manipulation and ignoring part of string -
i have input following text file using c++
command1 5 #create 5 box
length 12
inserttext box
how can read in input while ignoring after #sign?
for example output should without #create 5 box
command1 5 length 12 inserttext box
edit:
i tried following:
while(getline(myfile, line)) { istringstream readline(line); getline(readline, command, ' '); getline(readline, input, '\0'); }
...but doesn't seem work.
the outer while(getline(
, istringsteam
good, after you'd want read space-separated word command, perhaps 1 or more space-separated inputs after that: like
std::string command, input; std::vector<std::string> inputs; if (readline >> command && command[0] != '#') { while (readline >> input && input[0] != '#') inputs.push_back(input); // process command , inputs... }
it's easier use >>
getline
parse readline
because set stream failure state if don't @ least 1 valid character, making [0]
indexing safe , dropping out cleanly empty lines, or commands no inputs.
Comments
Post a Comment