C++ 读取文本文件并将数据插入数组

C++ 读取文本文件并将数据插入数组,c++,arrays,visual-studio,visual-studio-2010,visual-c++,C++,Arrays,Visual Studio,Visual Studio 2010,Visual C++,我找到的大部分信息都是基于数字的,但我想用文字。例如,如果我的文本文件如下所示: M Gordon Freeman Engineer F Sally Reynolds Scientist std::ifstream input("my file name"); std::vector<PeopleInfo> people; std::vector<PeopleInfo> p((std::istream_iterator<PeopleInfo>(input

我找到的大部分信息都是基于数字的,但我想用文字。例如,如果我的文本文件如下所示:

M
Gordon
Freeman
Engineer
F
Sally
Reynolds
Scientist
std::ifstream input("my file name");

std::vector<PeopleInfo> people;

std::vector<PeopleInfo> p((std::istream_iterator<PeopleInfo>(input)),
                          std::istream_iterator<PeopleInfo(),
                          std::back_inserter(people));
我希望能够将每一行放入一个数组,并按如下方式输出:

Gender: M
First Name: Gordon
Last Name: Freeman
Job: Engineer
Gender: F
First Name: Sally
Last Name: Reynolds
Job: Scientist
这个列表可以继续下去,但现在有两个是好的

我目前正在使用结构来保存信息:

struct PeopleInfo
{
    char gender; 
    char name_first [ CHAR_ARRAY_SIZE ];
    char name_last [ CHAR_ARRAY_SIZE ];
    char job [ CHAR_ARRAY_SIZE ];
};

我不确定是否需要使用分隔符或其他东西来告诉程序何时在每个部分停止(性别、名字、姓氏等)。我可以在ifstream中使用getline函数吗?我在自己的代码中实现它时遇到了困难。我真的不知道从哪里开始,因为我已经有一段时间没有使用过这样的东西了。疯狂地在教科书和谷歌上搜索,寻找类似的问题,但到目前为止,我运气不太好。我将用我发现的任何问题和代码更新我的帖子。

好的,让您开始:

// Include proper headers here
int main()
{
    std::ifstream file("nameoftextfilehere.txt");
    std::string line;
    std::vector<std::string> v; // Instead of plain array use a vector

    while (std::getline(file, line))
    {
        // Process each line here and add to vector
    }

    // Print out vector here
 }
//在此处包含正确的标题
int main()
{
std::ifstream文件(“nameoftextfilehere.txt”);
std::字符串行;
std::vector v;//使用向量代替普通数组
while(std::getline(文件,行))
{
//在这里处理每一行并添加到向量
}
//在这里打印向量
}

好的开始:

// Include proper headers here
int main()
{
    std::ifstream file("nameoftextfilehere.txt");
    std::string line;
    std::vector<std::string> v; // Instead of plain array use a vector

    while (std::getline(file, line))
    {
        // Process each line here and add to vector
    }

    // Print out vector here
 }
//在此处包含正确的标题
int main()
{
std::ifstream文件(“nameoftextfilehere.txt”);
std::字符串行;
std::vector v;//使用向量代替普通数组
while(std::getline(文件,行))
{
//在这里处理每一行并添加到向量
}
//在这里打印向量
}

在存储信息方面,结构可能比数组更好

struct person
{
    std::string gender;
    std::string first_name;
    std::string last_name;
    std::string position;
};

然后,您可以拥有一个人物向量,并对其进行迭代。

在存储信息方面,结构可能比数组更好

struct person
{
    std::string gender;
    std::string first_name;
    std::string last_name;
    std::string position;
};

然后你可以得到一个人物向量,并对其进行迭代。

你也可以使用像bool maleFlag和bool femaleFlag这样的标志,当你在一行上只读取'M'或'F'时,将它们设置为true和false,因此,您知道与后面的名称关联的性别。

您还可以使用类似bool maleFlag和bool femaleFlag的标志,当您在一行上只读“M”或“F”时,将它们设置为true和false,这样您就知道与后面的名称关联的性别。

您还可以将std::ifstream文件用作任何其他流:

//your headers
int main(int argc, char** argv)
{
    std::ifstream file("name.txt");
    std::string line;
    std::vector<std::string> v; // You may use array as well

    while ( file.eof() == false ) {
        file >> line;
        v.push_back( line );
    }

    //Rest of your code
    return 0;
}
//您的头
int main(int argc,字符**argv)
{
std::ifstream文件(“name.txt”);
std::字符串行;
std::vector v;//也可以使用数组
while(file.eof()==false){
文件>>行;
v、 向后推(线);
}
//代码的其余部分
返回0;
}
您还可以将std::ifstream文件用作任何其他流:

//your headers
int main(int argc, char** argv)
{
    std::ifstream file("name.txt");
    std::string line;
    std::vector<std::string> v; // You may use array as well

    while ( file.eof() == false ) {
        file >> line;
        v.push_back( line );
    }

    //Rest of your code
    return 0;
}
//您的头
int main(int argc,字符**argv)
{
std::ifstream文件(“name.txt”);
std::字符串行;
std::vector v;//也可以使用数组
while(file.eof()==false){
文件>>行;
v、 向后推(线);
}
//代码的其余部分
返回0;
}

我认为@user1200129走上了正确的轨道,但还没有把所有的部分都整合起来

我会稍微改变一下结构:

struct PeopleInfo
{
    char gender; 
    std::string name_first;
    std::string name_last;
    std::string job;
};
然后我会为该结构重载
操作符>

std::istream &operator>>(std::istream &is, PeopleInfo &p) { 
    is >> p.gender;   
    std::getline(is, p.name_first);
    std::getline(is, p.name_last);
    std::getline(is, p.job);
    return is;
}

由于您希望能够显示它们,我会添加一个
操作符,我认为@user1200129走上了正确的轨道,但还没有把所有的部分都放在一起

我会稍微改变一下结构:

struct PeopleInfo
{
    char gender; 
    std::string name_first;
    std::string name_last;
    std::string job;
};
然后我会为该结构重载
操作符>

std::istream &operator>>(std::istream &is, PeopleInfo &p) { 
    is >> p.gender;   
    std::getline(is, p.name_first);
    std::getline(is, p.name_last);
    std::getline(is, p.job);
    return is;
}

由于您希望能够显示它们,我将添加一个
操作符,好的,谢谢。我现在就玩这个。嗯,好的,谢谢。我现在要处理这个问题。对不起,忘记添加了,我正在使用结构。我添加了信息。我也会研究向量。对不起,忘记添加了,我使用的是结构。我添加了信息。我也会研究向量。
while(file>>行)
会更好。
while(file>>行)
会更好。我想你在
>
中漏掉了一些
p.