简体   繁体   English

如何从 C++ 文件中读取由空格分隔的全名?

[英]How do I read a full name that is separated by a space from a file in C++?

I am trying to read full names that are separated by space from a file.我正在尝试从文件中读取以空格分隔的全名。 In the example, name student .在示例中, name student Here is my code:这是我的代码:

class student{
    string name;
    int degree;
    int stage;
public:
    void input(){
        getline (my_file,name);
        my_file>>degree;
        my_file>>stage;
    }

    void display(){
        cout<<name<<"\n"<<degree<<"\n"<<stage<<endl;
    }
};

int main()
{
    student S[3];
    int len=0;
    my_file.open("D:/h.txt", ios::in);
    if (!my_file) {
        cout << "No such file";
    }
    else
        while (!my_file.eof()) {
            S[len].input();
            len++;
        }
    for(int i=0;i<3;i++)
        S[i].display();
    my_file.close();
    return 0;
}

my "h.txt" file:我的“h.txt”文件:

jack Donald
58
5
David William
82
4
Anthony Mark
89
3

My problem is, it don't show anything.我的问题是,它没有显示任何东西。

图片

Refer this:参考这个:

    void input() {
        getline(my_file, name);
        string blah;

        getline(my_file, blah);
        degree = std::stoi( blah);

        getline(my_file, blah);
        stage = std::stoi(blah);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 C++ 中的文件中读取用逗号分隔的全名? - How do I read a full name that is separated by a comma from a file in C++? 如何从控制台输入学生的全名(包括空格)并将其存储在c ++中的数组中? - How do I take full name of students(including space) as input from console and store it in an array in c++? 如何将空格分隔的文本文件读入 C++ 中的数组? - How do I read in a text file separated by spaces into an array in c++? 如何从文件中读取用空格分隔的数字 - How to read numbers separated with space from a file 如何通过在 c++ 中接收 std::istream object 来读取和解析逗号分隔的用户的输入? - How do I read and parse input from a user that is comma separated by receiving an std::istream object in c++? 如何从 c++ 中的文件读入 [暂停] - How do I read in from a file in c++ [on hold] 我如何从文件C ++读取并输出它 - How do i read from a file C++ and output it 如何从 c++ 中的文件读取输入 - How do I read inputs from file in c++ C ++-我阅读了一个完整的文件(_是由2个空格隔开的单词列表_),如何快速分别获得单词? - C++ - I read a whole file (_which is a list of words separated by 2 white spaces_), how do i get the words separately fast? C ++读取inFile,以逗号和空格分隔 - C++ Read inFile separated by a comma and a space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM