7 years, 11 months ago.

using istream_iterator bind to cin in STL, how to make the serial stream end?

when I send string to mbed board by pc COM port, the mbed serial stream can't terminate, in windows VC++ can make stream end by Ctrl-Z in command line, how to make an end of stream to serial in mbed?

istream_iterator

#include "mbed.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

DigitalOut myled(LED1);
vector<string> sentence;

int main() {
  copy(istream_iterator<string>(cin),
       istream_iterator<string>(),
       back_inserter(sentence));

  copy(sentence.begin(), sentence.end(),
       ostream_iterator<string>(cout," "));
  cout<<endl;
}

1 Answer

7 years, 11 months ago.

I think your best bet is to use some escape sequence or escape character... It might be that VC++ has mapped CTRL+Z to one of those. I'm using something like:

        std::cout << "\nPlease, enter some values (or quit to exit): ";
        std::istream_iterator<string> iit (std::cin);
        std::istream_iterator<string> eos;
        
        while (iit != eos) {
            std::cout << *(iit++);
            if ((*iit).compare("quit") == 0) {
                break;
            }
        }

what is "eos" ? how to implement in mbed?

posted by jerry liu 23 Jun 2017