【问题标题】:Reading from a text file and inserting data into an array从文本文件中读取数据并将数据插入到数组中
【发布时间】:2012-02-28 01:13:55
【问题描述】:

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

M
Gordon
Freeman
Engineer
F
Sally
Reynolds
Scientist

我希望能够将每一行放入一个数组并像这样输出:

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 函数吗?我无法在我自己的代码中实现它。我不确定从哪里开始,因为我已经有一段时间不用这样的东西了。疯狂搜索教科书和谷歌以找到类似的问题,但到目前为止我还没有多少运气。我会用我发现的任何问题和代码更新我的帖子。

【问题讨论】:

    标签: c++ arrays visual-studio visual-studio-2010 visual-c++


    【解决方案1】:

    我认为@user1200129 走在了正确的轨道上,但还没有把所有的部分都放在一起。

    我会稍微改变一下结构:

    struct PeopleInfo
    {
        char gender; 
        std::string name_first;
        std::string name_last;
        std::string job;
    };
    

    然后我会为该结构重载operator>>

    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;
    }
    

    既然您希望能够显示它们,我也会添加一个operator<< 来做到这一点:

    std::ostream &operator<<(std::ostream &os, PeopleInfo const &p) { 
        return os << "Gender: " << p.gender << "\n"
                  << "First Name: " << p.name_first << "\n"
                  << "Last Name: " << p.name_last << "\n"
                  << "Job: " << p.job;
    }
    

    然后读入一个充满数据的文件可以是这样的:

    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));
    

    同样,从矢量显示人们的信息类似于:

    std::copy(people.begin(), people.end(),
              std::ostream_iterator<PeopleInfo>(std::cout, "\n"));
    

    【讨论】:

    • 我认为您在 &gt;&gt; 中遗漏了一些 p.s。
    【解决方案2】:

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

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

    然后你可以有一个人向量并对其进行迭代。

    【讨论】:

    • 抱歉,忘记添加我使用的是结构体。我添加了信息。我也会研究向量。
    【解决方案3】:

    让你开始吧:

    // 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
     }
    

    【讨论】:

    • 嗯,好的,谢谢。我现在要玩这个。
    【解决方案4】:

    您还可以使用 bool maleFlag 和 bool femaleFlag 之类的标志,并将它们设置为 true 和 false,当您在一行中只读取“M”或“F”时,您就知道将什么性别与名称相关联接下来。

    【讨论】:

      【解决方案5】:

      您也可以将您的 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;
      }
      

      【讨论】:

      • while (file &gt;&gt; line) 会更好。
      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2019-05-15
      • 1970-01-01
      • 2013-12-10
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多