-1

I've made a function that receives a line and a delimiter, separates the line and returns a vector of floats (like the split function in java). This is the function:

vector<float> extractNumbers(string line, char delimiter) {
    vector<float> a;
    float f;
    string forNow = "";
    for (unsigned int i = 0; i < line.size(); i++) {
        if (line.at(i) == delimeter) {
            f = ::atof(forNow.c_str());
            cout << ::atof(forNow.c_str()) << endl;
            a.push_back(f);
            forNow = "";
        } else {
            forNow += line.at(i);
        }
    }
    f = ::atof(forNow.c_str());
    cout << f << endl;
    a.push_back(f);
    return a;
}

This is the text file I'm trying it with:

3 3
1 1 1
1 2 1
1 1 1

I call this function: vector<float> floatLine = extractNumbers(line, ' '); When I try to print forNow parameter I receive the numbers just like in the text, but when I print f or ::atof(forNow.c_str()) I receive a 0 instead of the first 3 in the first line. Any thoughts?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Javi
  • 889
  • 1
  • 16
  • 41
  • Why so complicated? Why not using the `std::istream& operator>>(std;;istream,double)` input operator instead? – πάντα ῥεῖ Apr 24 '15 at 15:10
  • I don't know this operator.. How would I use it? – Javi Apr 24 '15 at 15:12
  • Here's a good reference, including examples: http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt You can use it in combination with a [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream) you create from your `line` parameter. – πάντα ῥεῖ Apr 24 '15 at 15:14
  • @JaviDorfsman Perhaps it would be better if you write what you want to do with this file. –  Apr 24 '15 at 15:17
  • Works fine at http://ideone.com/X0rwvB. – R Sahu Apr 24 '15 at 15:18
  • I need to read a file, the first line is the heigh and width of the "kernel" the next lines are the "kernel" itself (I use opencv thus, "kernel"). I used this code in another work and it worked perfectly.. – Javi Apr 24 '15 at 15:18
  • @RSahu so, there is a problem in my file? – Javi Apr 24 '15 at 15:19
  • If you encounter more than one delimiter char in a row you're code will not behave how you want it, for example if there is a space in that first line in the third column. You'll hit the space after the 2nd '3', convert the '3', and then try to convert the ' ' on the next iteration. Also, if there is an unexpected leading space in your file you'll try to `atof("")` – mstbaum Apr 24 '15 at 15:21
  • In your posted code, you have `delimeter` instead of `delimiter`. Other than that, I don't see anything wrong with your file. – R Sahu Apr 24 '15 at 15:21
  • @RSahu, I fixed that.. For some reason it's still not working – Javi Apr 24 '15 at 15:23
  • Ok solved it.. the problem was in the file. Thanks everyone! – Javi Apr 24 '15 at 15:45

1 Answers1

2

Just if you don't know about such convenient way of interaction with files. You can use them like this:

float a, b;
float c, d, e;
float f, g, h;
fstream file("data.dat", ios::in);
if (file) {
    file >> a >> b;
    file >> c >> d >> e;
    file >> f >> g >> h;
    file.close();
}
Nolan
  • 1,060
  • 1
  • 11
  • 34