简体   繁体   English

从文件读取数据到结构数组

[英]Reading data into a struct array from a file

I have an input file that looks like this 我有一个看起来像这样的输入文件

 1 0 3
 2 11 5
 3 15 1
 4 16 11

and a structure that looks like this 和看起来像这样的结构

struct numb {
int numb1;
int numb2;
int numb3;
}

and I need to create an array of the struct so that each element of the array holds all three numbers. 并且我需要创建一个结构数组,以便该数组的每个元素都包含所有三个数字。 So 所以

numbArray[0].numb1 == 1
numbArray[0].numb2 == 0
numbArray[0].numb3 == 3
numbArray[1].numb1 == 2
numbArray[1].numb2 == 11

and so on. 等等。 I've gotten the hang of opening and closing files, finding how many lines there are in a file, and reading a single line from a file, but I do not know how to store individual elements from a line. 我已经费劲地打开和关闭文件,查找文件中有多少行,并从文件中读取一行,但是我不知道如何存储一行中的单个元素。

My program looks like this so far: 到目前为止,我的程序如下所示:

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
    ifstream inFile(argv[1]);
    int fileLength = 0;
    std::string line;
    while(std::getline(inFile, line))
    {
        ++fileLength;
    }
    struct numb {
    int numb1;
    int numb2;
    int numb3;
    }
    if(inFile.is_open())
    {
        for(unsigned i = 0; i <= fileLength; i++)
        {
            //What to do here?
        }
    }
}

Use getline when you don't have regular structure to the input and need to handle variation between lines. 当您的输入没有规则的结构并且需要处理行之间的变化时,请使用getline When your input file has regular structure (in this case, there are always three values per line), then simply use the stream extraction operators directly: 当您的输入文件具有规则结构(在这种情况下,每行始终有三个值)时,只需直接使用流提取运算符:

#include <iostream>
#include <vector>

struct group
{
    int n1;
    int n2;
    int n3;
};

int main()
{
    std::vector<group> groups;
    while (std::cin)
    {
        group line;
        line.n1 << std::cin;
        line.n2 << std::cin;
        line.n3 << std::cin;
        groups.push_back(group);
    }
}

Express your ideas directly in code as much as possible. 尽可能直接在代码中表达您的想法

Note I've written the code assuming that the file is in the proper form. 请注意,我已编写代码,假定文件格式正确。 If there are too many or too few values per line, then the above code will be confused. 如果每行中的值太多或太少,那么上面的代码将被混淆。 However, it is best to code the simplest thing that could possibly work and worry about complexity when you need it. 但是,最好编写可能会起作用最简单的代码,并在需要时担心复杂性。 In your example you stated that the input file was well-formed, so there's no need to overcomplicate things. 在您的示例中,您指出输入文件的格式正确,因此不需要使事情复杂化。

I recommend using a std::stringstream for this: 我建议为此使用std::stringstream

#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <vector>

struct numb {
  int numb1;
  int numb2;
  int numb3;
};

void populate(std::vector<numb>& my_numbs, std::string line) {
   std::stringstream ss(line);
   numb my_numb;
   ss >> my_numb.numb1 >> my_numb.numb2 >> my_numb.numb3;
   my_numbs.push_back(my_numb);
}

void output(const numb my_numbs) {
    printf("%d %d %d\n", my_numbs.numb1, my_numbs.numb2, my_numbs.numb3);
}

int main(int argc, char* argv[]) {
  ifstream inFile(argv[1]);
  std::string line;
  std::vector<numb> my_vect;
  while(std::getline(inFile, line)) {
    populate(my_vect, line);
  }

  for(size_t i = 0; i < my_vect.size(); ++i) {
    std::cout << "my_vect[" << i << "]:";
    output(my_vect[i]);
  }

  return 0;
}

std::stringstream s allow you to parse out data types from std::string s, you you just need to parse out 3 int s, which you can use with your struct. std::stringstream允许您从std::string解析出数据类型,您只需要解析3个int即可与您的结构一起使用。 You then push the struct into your vector. 然后,将结构推入向量。

Here's the working ideone taking input from stdin. 这是正在工作的ideone接受stdin的输入。

You should probably be able to do something like this: 您应该应该可以执行以下操作:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
    ifstream inFile(argv[1]);
    int fileLength = 0;
    std::string line;
    struct numb {
        int numb1;
        int numb2;
        int numb3;
    };
    vector<vector<int>> sets;
    int n1, n2, n3;
    while (std::cin >> n1)
    {
        cin >> n2;
        cin >> n3;
        vector<int> vec;
        vec.push_back(n1);
        vec.push_back(n2);
        vec.push_back(n3);
        sets.push_back(vec);
    }

    numb * numbSet = new numb[sets.size()];

    //Since the vectors data is continuous in memory just as the array of structs are
    //you can just copy the data directly
    for (int i = 0; i < sets.size(); i++)
    {
        std::memcpy(&numbSet[i], &sets[i][0], sizeof(numb));
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM