Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文件读取而不是从字符串流读取_C++_Io - Fatal编程技术网

C++ 从文件读取而不是从字符串流读取

C++ 从文件读取而不是从字符串流读取,c++,io,C++,Io,我正在尝试从以下格式的文本文件中读取: mdasd tgfor,100,23 sfier sdfdor,2,3 mmmmmm,232,452 使用此功能:我以各种方式进行测试,但无法使其正常工作: void StudentRepository::loadStudents(){ ifstream fl; fl.open("studs.txt"); Student st("",0,0); string str,s; stringstream ss;

我正在尝试从以下格式的文本文件中读取:

mdasd tgfor,100,23
sfier sdfdor,2,3
mmmmmm,232,452
使用此功能:我以各种方式进行测试,但无法使其正常工作:

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();
}
您可以在带有自定义分隔符的
std::stringstream
上使用
getline()
,如下所示:

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;
    }
}
if(fl.is_open()){
while(getline(fl,str)){
stringstream ss(str);
字符串名称、id、gr;
while(ss){
getline(ss,name,,);//,“作为分隔符
getline(ss,id,,);
getline(ss,gr,,);
}
cout您可以在带有自定义分隔符的
std::stringstream
上使用
getline()
,如下所示:

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;
    }
}
if(fl.is_open()){
while(getline(fl,str)){
stringstream ss(str);
字符串名称、id、gr;
while(ss){
getline(ss,name,,);//,“作为分隔符
getline(ss,id,,);
getline(ss,gr,,);
}

你不能在这里使用stringstream吗?我不知道它有什么好处。要将fro-mstring转换回int:\你应该在这里使用stringstream吗?我不知道它有什么好处。要将fro-mstring转换回int:\你仍然必须从
std::string
s获得
s的想法谢谢…我有很多问题ems,因为我的手工子定界器等等,但我现在工作得很好:)再次感谢,您仍然需要从
std::string
s获得
int
s思想管理谢谢…我因为我的手工子定界器等原因遇到很多问题,但我现在工作得很好:)再次感谢,