0

I'm learning c++, please be gentle. Am struggling to find my feet after too long with Ruby.

Basically, I'm trying to get a http response, split it up and then save various parts as variables.

I'm using happyhttp instead of curl because it's smaller and we can't use boost as recommended in other SO posts.

The response we get looks like this:

Nzov1td_xs4MNlnTd5sU:60:60:websocket,htmlfile,xhr-polling,jsonp-polling

I've tried putting in in stringstream and then using getline but that obviously just outputs the lines:

stringstream os;
os << data;
string s = os.str();
std::string segment;
while(getline(os, segment, ':'))
{
    cout << segment;
}

I need something like this (I know the syntax is wrong):

socketID = segment[0]
timeout = segment[1]
heartbeat = segment[3]

Genuinely don't know what I should be looking for to get this working?

simonmorley
  • 2,810
  • 4
  • 30
  • 61

2 Answers2

2

You're on the right track, you just haven't got the right container.

To continue with a solution from one of the containers in my comment:

std::array<std::string, 4> segments;
int i = 0;
while (std::getline(os, segment, ':'))
    segments[i++] = segment;

Or if you don't have std::array, a solution based on std::vector:

std::vector<std::string> segments;
while (std::getline(os, segment, ':'))
    segments.push_back(segment);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you. Let me try now :) – simonmorley Jul 04 '13 at 13:52
  • Yeah, don't have array but vector works. How can I go about saving each segment to a specific variable though? Thanks for the help, appreciated. – simonmorley Jul 04 '13 at 13:59
  • @simonmorley Just like you write in your question... `std::string heartbeat = segments[2];` If you want it as an integer, check out either [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) or [`std::strtol`](http://en.cppreference.com/w/cpp/string/byte/strtol) (you probably have to use the latter since if you don't have `std::array` then you most likely don't have `std::stoi` either). – Some programmer dude Jul 04 '13 at 14:01
  • Amazing, thank you. Battling with this after the mollycoddled world of ruby, python and js. Have a good day, S. – simonmorley Jul 04 '13 at 14:03
0

You can use sscanf() to parse the tokens you need. As a bonus it's faster than the C++ stuff.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • C++ provides the same functionality as sscanf but with OO approach, it isn`t good idea to mix c/c++ – spin_eight Jul 04 '13 at 13:48
  • OO is not always better, and sscanf is a lot faster than C++ iostreams. Do you have anything concrete or just think it isn't a "good idea"? – John Zwinck Jul 04 '13 at 13:50
  • Well, C++ methods are "safer" because they take care of the types, and won't sodomize you if you change a %f to a %d by mistake. Having said that, if you're an alert programmer, C methods are definitely, significantly faster, especially if you have a lot of file I/O. – John Doe Jul 04 '13 at 14:19
  • GCC or Clang will save you from typing %d instead of %f. Just enable the format warnings (yeah, they're off by default, just like all the useful warnings in C and C++...). – John Zwinck Jul 04 '13 at 14:32
  • @John Zwinck Yes, I do. 1. sscanf function is not secure because allows buffer overflow, which can lead to code injection for example. So instead sscanf_s can be used. 2. Question wasn`t labeled "C" therefore it will be logical to give answer in the C++ area. – spin_eight Jul 05 '13 at 05:34