C ++:读CSV文件导入结构数组 [英] C++: Reading CSV file into struct array

查看:197
本文介绍了C ++:读CSV文件导入结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作分配,我需要阅读未知的行数的CSV文件到一个结构化的阵列。只能通过C ++,不是C(他们不希望我们把两者结合起来)。

所以,我有以下code:

  //定义
结构项目{
    INT ID;
    字符串名称;
    desc字符串;
    串价格;
    串个;
};步骤1无效(){    串namefile,线;
    INT计数器= 0;    COUT<< 该文件的名称:<< ENDL;
    CIN>> namefile;    ifstream的文件;    file.open(namefile);    如果(!file.is_open()){        COUT<< 文件<< namefile<<未找到。 << ENDL;
        出口(-1);    }    而(函数getline(文件,行)){//为了得到该文件中的行数
        反++;
    }    项目*项目=新项目[窗口]; //添加一些结构化阵列    的for(int i = 0; I<反向;我++){        文件>>项目[I] .ID>>项目[我]。名称>>项目[I] .desc>>项目[I]。价格>>项目[I] .PCS;    }    COUT<<项目[1]。名称<< ENDL;    file.close();
}

但是,当我运行code,应用程序将阅读和实际上我觉得它不读都返回后的空间。下面是在控制台输出:

 的文件的名称:
open.csv程序,退出code结束:0


解决方案

通过您第一个循环读取流。当有没有别的留下来读的第一循环停止。在这一点上流进入故障模式(即的std :: ::的ios_bas​​e failbit 获取设置,它会拒绝读什么,直到它被莫名其妙地恢复。

您可以恢复文件中使用文件GOID状态。清()。这本身就不会有所帮助,但是,作为流仍处于末端。你可以设法开始阅读之前,但我不会做abything那样!相反,我只想读取文件在一个通和的push_back()每个元素到 SYD ::矢量<项目>

请注意,你必须为每个项目输入记录可能并不完全做你想要它做的事情:如果你真的有一个CSV文件,你需要阅读的分隔符(如),并读取ID之后忽略的分隔符。此外,你应该总是看完后测试流的状态。你的循环可以如看起来soemething是这样的:

 为(我的物品;
      (文件>> i.id).IGNORE(的std :: numeric_limits<的std :: streamsize可> :: MAX()',')
      &功放;&安培;的std ::函数getline(文件,i.name,',')
      &功放;&安培;的std ::函数getline(文件,i.desc,',')
      &功放;&安培;的std ::函数getline(文件,i.price,',')
      &功放;&安培;的std ::函数getline(文件,i.pcs); ){
    is.push_back(ⅰ);
}

现在需要的正是一定程度上取决于确切的文件格式。

I am working on an assignment where I need to read a CSV file of unknown number of lines into an structured array. Only via C++, not C (they don't want us to combine both).

So, I have following code:

// DEFINITION
struct items {
    int ID;
    string name;
    string desc;
    string price;
    string pcs;
};

void step1() {

    string namefile, line;
    int counter = 0;

    cout << "Name of the file:" << endl;
    cin >> namefile;

    ifstream file;

    file.open(namefile);

    if( !file.is_open()) {

        cout << "File "<< namefile <<" not found." << endl;
        exit(-1);

    }

    while ( getline( file, line) ) { // To get the number of lines in the file
        counter++;
    }

    items* item = new items[counter]; // Add number to structured array

    for (int i = 0; i < counter; i++) {

        file >> item[i].ID >> item[i].name >> item[i].desc >> item[i].price >> item[i].pcs;

    }

    cout << item[1].name << endl;

    file.close();
}

But when I run the code, the app will return space after reading and I actually think it's not reading at all. Here is the output in the console:

Name of the file:
open.csv

Program ended with exit code: 0

解决方案

With you first loop read the stream. The first loop stops when there is nothing else left to read. At that point the stream gets into failure mode (i.e., std::ios_base::failbit gets set and it will refuse to read anything until it gets somehow restored.

You can restore a file to goid state using file. clear(). That alone won't help, though, as the stream is still at its end. You could seek to the start before reading but I wouldn't do abything like that! Instead, I would just read the file in one pass and push_back() each element to a syd::vector<items>.

Note that the input you have for each items record probably doesn't quite do what you want it to do: if you really have a CSV file you'll need to read to the separators (e.g. the ,) and ignore the separator after reading the ID. Also, you should always test the state of the stream after reading. Your loop could e.g. look soemething like this:

for (items i;
      (file >> i.id).ignore(std::numeric_limits<std::streamsize>::max(), ',')
      && std::getline(file, i.name, ',')
      && std::getline(file, i.desc, ',')
      && std::getline(file, i.price, ',')
      && std::getline(file, i.pcs); ) {
    is.push_back(i);
}

What is needed exactly somewhat depends on the exact file format.

这篇关于C ++:读CSV文件导入结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆