English 中文(简体)
从txt文件读取和创建对象
原标题:Reading & creating an object from txt file
  • 时间:2012-05-27 16:01:30
  •  标签:
  • c++

我有衰减功能,我不明白为什么它不起作用。保存后,数据被正确保存,但在读取时,它不会读取int部分。

void StudentRepository::loadStudents(){
    ifstream fl;
    fl.open("studs.txt");
    Student st("",0,0);
    string str,s;
    stringstream ss;
    int i;
    int loc;
    if(fl.is_open()){
        while(!(fl.eof())){
            getline(fl,str);
            loc = str.find(",");
            ss << str.substr(0,loc);
            s = ss.str();
            st.setName(s);
            str.erase(0,loc);
            loc = str.find(",");
            ss << str.substr(0,loc);
            ss >> i;
            st.setId(i);
            str.erase(0,loc);
            ss >> i;
            st.setGroup(i);
            students.push_back(st);

        }
    }
    else{
        cout<<"~~~ File couldn t be open! ~~~"<<endl;
    }
    fl.close();
}

编辑

class Student {
private:
    string name;
    int ID;
    int group;



public:
Student(string name, int id, int gr ):name(name),ID(id),group(gr){}

void setId(int value)       {group = value;}
void setGroup(int value)    {ID = value;}
void setName(string value)  {name = value;}
int getGroup()              const{return group;}
int getID()              const{return ID;}
    string getName()            const{return name;}

    friend ostream& operator << (ostream& out, const Student& student)
    {
        out << student.name << " " << student.ID << " " << student.group <<endl;
        return out;
    }
};

file:(在打印时,当我在load函数中初始化对象时,所有int都是0)

maier tsdar,2,0
staedfsgu aldasn,0,3
pdasdp aasdela,323,23
marsdaciu baleen,234,4534
madsd,234,2345
问题回答

当你调用时,s=ss.str(),它不消耗缓冲区,因此下次您尝试提取int时,提取失败,因为ssbuffer仍然包含初始字符串(而不仅仅是您在末尾附加的数字的字符串表示)。您可以为int提取创建一个新的字符串流对象,或者在尝试提取ints之前消耗所有内容





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签