English 中文(简体)
在构造器中实施一个函数
原标题:Implementing in the Constructor a function
  • 时间:2012-05-26 16:57:06
  •  标签:
  • c++
  • io

我试图从文件读取一些我已有的数据, 并将其装入矢量。 然后我试图将更改保存到文件( 有效 ) 。

但当我运行程序时,它不读我的档案。这是我的代码:

保存/ 装入函数 :

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

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


void StudentRepository::saveStudents(){
    ofstream fl;
    fl.open("studs.txt");
    if(fl.is_open()){
        for(unsigned i=0; i<students.size(); i++){
            fl<<students[i].getName();
            fl<<",";
            fl<<students[i].getID();
            fl<<",";
            fl<<students[i].getGroup();
            fl<<endl;
        }
    }
    else{
        cout<<"~~~ File couldn t be open! ~~~"<<endl;
    }
}

我将负载函数称为内存中的矢量:

StudentRepository::StudentRepository(){
loadStudents();
}

任何想法什么/我哪里做错了什么, 或一些建议来纠正它?

最佳回答

您似乎正确读取了学生信息 。 但是您不会在任何矢量中添加读信息 。 您应该在您的 < code> load Students 方法中包含类似 < code> students 的信息 。 此外, 在循环开始时初始化 < code>st 更好。

问题回答

暂无回答




相关问题
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?

热门标签