-2

I have a file which is formatted in the following way:

0x10c3 0xad6066
0x10c7 0xad6066
0x10c1 0xad6066
0x10c5 0xad6066
0x10c3 0xad6066

I want to read the first value into an array input[] and the second into array param[].

I tried the following:

while(getline(f, line)){
    stringstream ss(line);
    getline(ss, input[i], ' ');
    getline(ss, param[i]);
}

The error I am receiving is the following error: no matching function for call to 'getline(std::stringstream&, uint16_t&, char)'

I am trying to save a string into a integer array. So, how do I save the string into an integer array.

Invariance
  • 313
  • 6
  • 16

3 Answers3

2

You can do it like this:

while(getline(f, line)){
    // splitting
    std::string first = line.substr(0, line.find(" "));
    std::string last = line.substr(line.find(" ") + 1);

    // print result
    std::cout<<"First: " << first << std::endl << "Last: " << last << std::endl;
}
aholm
  • 86
  • 3
2
#include <iostream>
#include <sstream>
#include <iomanip>
#include <memory>
#include <vector>
#include <iterator>
#include <algorithm>

struct Foo {
    int a;
    int b;
};

std::istream &operator>>(std::istream &input, Foo &data)
{
    return input >> std::hex >> data.a >> std::hex >> data.b;
}

std::ostream &operator<<(std::ostream &output, const Foo &data)
{
    return output << std::hex << data.a << " - " << std::hex << data.b;
}

template<typename T>
std::istream &readLineByLine(std::istream &input, std::vector<T> &v)
{
    std::string line;
    while(std::getline(input, line)) {
        std::istringstream lineStream { line };
        T data;
        if (lineStream >> data) {
            v.push_back(data);
        }
    }
    return input;
}


int main() {
    std::vector<Foo> v;
    readLineByLine(std::cin, v);

    std::copy(v.begin(), v.end(), std::ostream_iterator<Foo>{ std::cout, "\n" });

    return 0;
}

https://wandbox.org/permlink/6PLekmBL5kWPA9Xh

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • what question does this answer? / what problem does this solve? – 463035818_is_not_an_ai Oct 02 '18 at 11:51
  • Question is crappy, but if you know what problems newbies have then you can predict what he relay needs (sometimes it is easier then asking to fix question). Ant this answers: how to read hex values, read line by line, how to store data in something else than C array. – Marek R Oct 02 '18 at 11:54
  • 1
    Your function `readLineByLine()` is quite pointless since you have `operator>>` for `Foo`s and obviously know how to use iterators. `std::vector v{ std::istream_iterator{is}, std::istream_iterator{} };` ftw. – Swordfish Oct 02 '18 at 11:56
  • kinda agree. Imho your answer would be improved by adding a sentence along the line of "Reading the values into strings seems to be fine in your code (assuming `input` and `param` are arrays of strings) and here is how to get the numbers:" – 463035818_is_not_an_ai Oct 02 '18 at 11:57
0

If your arrays defined to store (unsigned) integer values (e.g. unsigned int input[MAX], param[MAX];) you can use std::istringstream, e.g.:

std::istringstream iss;
for(int i = 0; i < MAX; i++){
     if(!getline(f, line)) break;
     iss.str(line);
     iss >> std::hex >> input[i] >> param[i];
     std::cout<< std::hex << input[i] << " " << param[i] << std::endl;
}

Do not forget to add

#include <sstream>

in your source code file.

VolAnd
  • 6,367
  • 3
  • 25
  • 43