Read in from file into structure

寵の児 提交于 2019-12-24 19:35:10

问题


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();

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20433370/read-in-from-file-into-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!