views:

358

answers:

1

I have a c++ program that takes input from a linux pipe, and outputs to std::cout for further processing.

Currently my code looks like this:

std::istreambuf_iterator<char> it(std::cin);
std::istreambuf_iterator<char> end;
std::string str(it, end);

//Lots of string manipulation here.

str = str.substr(0, 65535);
std::cout << str << std::endl;

This is wasteful because the incoming strings are huge.

  1. How do I only read around 100kB of cin to a c++ string and ignore the rest?
  2. How can I truncate the c++ string to 65535 bytes instead of characters? (I'll need to handle multi-byte characters in future.)
+2  A: 
  1. std::cin.read(&buf, 100*1024);
  2. std::string.size() returns num bytes, I'm fairly sure.
Marcus Lindblom
Yeah, std::string thinks in bytes; it doesn't know UTF-8 from codepage 932
Ben Straub