user3026386
user3026386

Reputation: 55

Read in from file into structure

I want to read in from txt file into structure using fstream. I save the data to the file in the way shown below: To read the data i tried some cheeky stuff with getlines or tabsin<

struct tab{
    int type,use;
    string name, brand;

};

tab tabs[500];

ofstream tabsout;
tabsout.open("tab.txt", ios::out);  
for (int i = 0; i < 500; i++){
    if (tabs[i].use==1){

        tabsout << tabs[i].type << " " << tabs[i].name << " " << tabs[i].brand << "\n";

    }
}

tabsout.close();

//input part that fails me :(

    int i=0;
    ifstream tabsin;
    tabsin.open("tab.txt", ios::in);
    if (tabsin.is_open()){
    while(tabsin.eof() == false)
    {
        tabsin >> tabs[i].type>>tabs[i].name>>tabs[i].brand;
        i++
    }

    tabsin.close();

Upvotes: 0

Views: 1109

Answers (3)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

For starters, do not use .eof() to control your loop: it doesn't work. Instead, use the stream's state after reading:

int type;
std::string name, brand;
while (in >> type >> name >> brand) {
    tabs.push_back(tab(type, name, brand));
}

If your name or brand contain spaces, the above won't work and you will need to write a format where you can know when to stop abd read correspondingly, e.g., using std::getline().

You might also consider wrapping the logic to read or write an object by suitable operators.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490138

You usually want to overload operator>> and operator<< for the class/struct, and put the reading/writing code there:

struct tab{
    int type,use;
    string name, brand;

    friend std::istream &operator>>(std::istream &is, tab &t) { 
        return is >> t.type >> t.name >> t.brand;
    }

    friend std::ostream &operator<<(std::ostream &os, tab const &t) { 
        return os << t.type << " " << t.name << " " << t.brand;
    }
};

Then you can read in a file of objects like:

std::ifstream tabsin("tab.txt");
std::vector<tab> tabs{std::istream_iterator<tab>(tabsin), 
                      std::istream_iterator<tab>()};

....and write out the objects like:

for (auto const &t : tabs) 
    tabsout << t << "\n";

Note that (like any sane C++ programmer) I've used a vector instead of an array, to (among other things) allow storing an arbitrary number of items, and automatically track how many are actually being stored.

Upvotes: 3

user2637317
user2637317

Reputation:

istream& getline (istream&  is, string& str, char delim);

Take a look at the third parameter, you can use std::getline to parse your line. But that is definitely not the best way to serialize objects. Instead of using a text file, you should use a byte stream.

Upvotes: 0

Related Questions