Saving lines in file to variable.

I'm trying to save data from a file to variables in my code and it is not working as intended.

Here is the code

#include <fstream>
#include <iostream>

using namespace std;

void className::readFile(const string file)
{
ifstream in;
in.open(file);

if (in.is_open()) {
in >> numOfObj;
for (int i = 0; i < numOfObj; i++) {
getline(in, var1);
in >> var2;
in >> var3;
in.ignore();
this->addAlarm(var1, var2, var3);
}
in.close();
}
}

None of the data is saved from my file to my variables.

File looks like this:

3
kej
3
3
nej
2
2
hej
5
5

Last edited on
There's a lot that needs to be assumed here. But if the rest of the code is ok, here's a question: how do the values get transferred from var1, var2, var3 to fileModell, filePrice, fileMonth?
And you don't tell us anything about what the file looks like, etc
http://www.catb.org/esr/faqs/smart-questions.html
updated. Sry, it's my first topic.
The following program reads the file data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# include <iostream>
# include <string>
# include <fstream>
# include <vector>
# include <utility>

struct someStruct
{
    std::string m_model;
    double m_price;
    unsigned int m_month;

    someStruct(const std::string& model, const double price, const unsigned int month) :
        m_model(model), m_price(price), m_month(month){}
};

std::ostream& operator << (std::ostream& os, const someStruct& s)
{
    os << s.m_model << " " << s.m_price << " " << s.m_month << "\n";
    return os;
}

int main()
{
    std::ifstream inFile{"F:\\test.txt"};
    std::vector<someStruct> someStruct_vec{};

    if(inFile)
    {
        std::string dummy{};
        getline(inFile, dummy);
        while (inFile)
        {
            std::string model{};
            double price{};
            unsigned int month;
            inFile >> model >> price >> month;
            someStruct s(model, price, month);
            if(inFile)someStruct_vec.push_back(std::move(s));
        }
    }
    for (const auto& elem : someStruct_vec)std::cout << elem;
}

/* Output: 
kej 3 3
nej 2 2
hej 5 5
*/
This is not helping me understand. Am I using my ifstream wrong... ? I don't understand your code.
I don't understand ...

very open-ended, what is it that you don't understand and what have your tried towards understanding? Did you look up each of the unfamiliars? Be more specific and don't expect to be spoon-fed
As far as I can tell, the main problems are
(a) missing ignore() after in >> numOfObj;
(b) no checking of file status after attempting to read from it.

Here's a complete example. Note, I had to use my imagination to guess what the rest of the code might be like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>

using namespace std;

struct Item {
    string a;
    int b;
    int c;

};

class className {

public:
    void readFile(const string file);    
    void addAlarm(string var1, int var2, int var3);
    void print() const;
    
private:
    vector<Item> data;
        
};

void className::readFile(const string file)
{
    ifstream in;
    in.open(file);
    
    if (in.is_open()) 
    {
        int numOfObj;
        string var1;
        int var2;
        int var3;
        
        in >> numOfObj;
        in.ignore(100, '\n');   // added ignore here
        
        for (int i = 0; i < numOfObj; i++) 
        {
            getline(in, var1);
            in >> var2;
            in >> var3;
            if (in)
            {
                in.ignore(100, '\n');   // modified ignore here
                this->addAlarm(var1, var2, var3);
            }
            else
            {
                cout << "readFile failed. i = " << i << '\n'; 
            }
        }
        in.close();
    }
}

void className::print() const
{
    cout << "number of items: " << data.size() << '\n';
    for (const auto & item : data)
    {
        cout << setw(20) << item.a 
             << setw(10) << item.b
             << setw(10) << item.c
             << '\n';
    }
}

void className::addAlarm(string var1, int var2, int var3)
{
    data.push_back( {var1, var2, var3} );
}

int main()
{
    className test;
    test.readFile("data.txt");
    test.print();
}

"Be more specific and don't expect to be spoon-fed " - you're right of course. I got a bit carried away, I wanted to test the code before suggesting the required fixes to be made.

But really, the onus is on the OP to post some code which can be compiled and run.
It was missing in.ignore. That was it.
Topic archived. No new replies allowed.