Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正在从缺少列的文件中读取数据。C++;_C++_Arrays_File - Fatal编程技术网

C++ 正在从缺少列的文件中读取数据。C++;

C++ 正在从缺少列的文件中读取数据。C++;,c++,arrays,file,C++,Arrays,File,实际上,我有一个包含8列的数据文件,我想把每列中的每个值放入一个数组变量中。但问题是缺少一些价值观。 e、 g 缺少的值用额外的制表符或/t空格隔开。 如何读取值,忽略缺失的数据,并在可用时将正确的值输入到正确的变量中 我尝试使用: infile >> network; string val = isNaN(network); if (count % 8 == 0) { ID[count / 8] = val; } if (count % 8 == 1) {

实际上,我有一个包含8列的数据文件,我想把每列中的每个值放入一个数组变量中。但问题是缺少一些价值观。 e、 g

缺少的值用额外的制表符或/t空格隔开。 如何读取值,忽略缺失的数据,并在可用时将正确的值输入到正确的变量中

我尝试使用:

infile >> network;
    string val = isNaN(network);
    if (count % 8 == 0) { ID[count / 8] = val; }
    if (count % 8 == 1) { time[count / 8] = val; }
    if (count % 8 == 2) { country_code[count / 8] = val; }
    if (count % 8 == 3) { sms_in[count / 8] = val; }
    if (count % 8 == 4) { sms_out[count / 8] = val; }
    if (count % 8 == 5) { call_in[count / 8] = val; }
    if (count % 8 == 6) { call_out[count / 8] = val; }
    if (count % 8 == 7) { internet[count / 8] = val; }
    count++; 

< > C++中的一个好方法是使用GETLIN获取每一行。
#include <string>
#include <vector>
...
typedef struct {
    unsigned long id;
    unsigned long timestamp;
    ...
} Record;
std::vector<Record> records;
while (std::getline(std::cin, s)) {
    ...
索引i是记录索引,从0开始。对于浮点数和双精度浮点数,您需要用适当的标准数字解析器替换std::atoi

如果数据以制表符分隔,则对于每个字段(每个记录),使用find_first_of(posTab+1,“/t”)查找每个字段的开头,从可以保存在posPreviousTab中用作相等性测试和第一个substr参数(而不是零)的上一个位置开始

注释

对于大型数据集,在某些情况下,std::list比std::vector快。您可以编写一个测试来比较案例中的两个选项


如果你处理大数据,你可能需要更高的速度,在C中使用char [Max St]和等效算法,而不是在内存中存储每个记录。

< P>一个好方法,在C++中使用GETLIN获取每一行。< /P>
#include <string>
#include <vector>
...
typedef struct {
    unsigned long id;
    unsigned long timestamp;
    ...
} Record;
std::vector<Record> records;
while (std::getline(std::cin, s)) {
    ...
索引i是记录索引,从0开始。对于浮点数和双精度浮点数,您需要用适当的标准数字解析器替换std::atoi

如果数据以制表符分隔,则对于每个字段(每个记录),使用find_first_of(posTab+1,“/t”)查找每个字段的开头,从可以保存在posPreviousTab中用作相等性测试和第一个substr参数(而不是零)的上一个位置开始

注释

对于大型数据集,在某些情况下,std::list比std::vector快。您可以编写一个测试来比较案例中的两个选项


如果您处理的是大数据,您可能需要更高的速度,请使用char[MAXSIZE]和C中的等效算法并动态处理,而不是将每条记录存储在内存中。

您是否听说过
开关
?请同时指定文件的格式,选项卡和空格不含糊。这确实有帮助,但我不完全理解如何使用它。您是否听说过
开关
?请同时指定文件格式,选项卡和空格不含糊。这确实有帮助,但我不完全理解如何使用它。
posTab = s.find_first_of('\t');
records[i].id = posTab == 0
    ? defaultID
    : std::atoi(s.substr(0, posTab).c_str());