English 中文(简体)
从未读取字符串流的文件读取
原标题:Reading from file not reading stringstream
  • 时间:2012-05-27 17:22:18
  •  标签:
  • c++
  • io

我试图从表格的文本文档中读取:

mdasd tgfor,100,23
sfier sdfdor,2,3
mmmmmm,232,452

3⁄4 ̄ ̧漯B

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(getline(fl,str)){
            cout<<"string: "<<str<<endl;

            loc = str.find(",");
            ss << str.substr(0,loc);
            s = ss.str();

            cout<<"name: "<<s<<endl;

            st.setName(s);
            ss.str("");
            str.erase(0,loc+1);

            loc = str.find(",");
            ss << str.substr(0,loc);
            ss >> i;

            cout<<"id:"<<i<<endl;

            st.setId(i);
            ss.str("");
            str.erase(0,loc);
            ss >> i;
            st.setGroup(i);
            ss.str("");

            cout<<"gr:"<<i<<endl;
            students.push_back(st);

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

但是,因为我可以告诉你在运行中, 它只读取第一行的名字 比一切都是空白的:

string: mqqeeqw tuwqer,23,32 /// ID AND GROUP are initializate with 0 thats 
name: mqqeeqw tuwqer         /// why they have that values... why does it not work?
id:0
gr:0

string: maier tudor,2,3
name: 
id:0
gr:0

string: maier tudor,0,0
name: 
id:0
gr:0
最佳回答

您可以在 std:::stringstream 上使用 ggetline () , 并使用自定义的分隔符, 类似 :

if(fl.is_open()){
    while(getline(fl,str)){
        stringstream ss(str);
        string name, id, gr;
        while(ss) {
            getline(ss, name,  , );  // "," as delimiter
            getline(ss, id,  , );
            getline(ss, gr,  , );
        }

        cout << "name: " << name <<endl;
        cout << "id:" << id << endl;
        cout << "gr:" << gr << endl;
        cout << endl;
    }
}
问题回答

暂无回答




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

热门标签