Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.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++_File_Find_Ifstream_Getline - Fatal编程技术网

C++ 计算文件C+中有多少数据组+;

C++ 计算文件C+中有多少数据组+;,c++,file,find,ifstream,getline,C++,File,Find,Ifstream,Getline,我有一个这样的数据文件 # Some Word n: 3 other word # Paarthunax is over age 100 2.230 4.940 1.934 4.328 3.340 4.470 4.023 3.546 5.734 3.570 2.194 2.147 # Some Word # Some other Words 123 # Words a: 23 1.232323 : 12312321.1

我有一个这样的数据文件

  # Some Word n: 3   other word
  # Paarthunax is over age 100
  2.230  4.940  1.934  4.328
  3.340  4.470  4.023  3.546
  5.734  3.570  2.194  2.147
 

  # Some Word
  # Some other Words      123
  # Words a: 23    1.232323  : 12312321.123123    
  1.132  2.323  4.323  3.342
  1.131  1.233  5.232  4.432
  1.131  3.123  5.232  4.432

  1.131  1.123  4.232  5.442      
  1.134  3.333  2.423  4.312
  1.135  2.143  1.242  1.412    
我的数据文件包含如此多的数据组; 每个数据组都有描述行(以#开头的行)。数据组中可能有空行,编号未知。数据组的描述行数未知(可能是任何内容)。数据组之间的空行数未知,数据组行数未知。 数据行可能采用科学符号(1.230E-01)

我不想从中读取数据,我已经在用矢量手动执行了我只需要计算文件中有多少数据组,但我无法为此类文件找出合理的模式

我为此定义了三个函数

unsigned int get_number_of_lines(const string& file_name){
unsigned int number_of_lines = 0;
string all_lines;
ifstream file(file_name);
while(getline(file, all_lines)){
    ++number_of_lines;
}
file.close();
return number_of_lines;
}
bool is_header(string line) {
    bool a = (line.find("#") != string::npos);
    return a;
}
int get_number_of_data_lists(string filename) {
int number_of_datalist;
ifstream stream(filename);
string line, subLine;
int i = 0;
while (i < get_number_of_lines(filename)){
    getline(stream, line);
    if(is_header(line)){
      ifstream sub_stream(filename);
      getline(sub_stream, subLine);
      while (is_header(subLine)){
            }
        }
    }
return number_of_datalist;
   }
无符号整数获取行数(常量字符串和文件名){
_行的无符号整数编号=0;
把所有的线串起来;
ifstream文件(文件名);
while(getline(文件,所有_行)){
++_线的数量;
}
file.close();
返回\u行的编号\u;
}
bool is_标题(字符串行){
boola=(line.find(“#”)=string::npos);
返回a;
}
int获取数据列表的编号(字符串文件名){
数据列表的整数;
ifstream(文件名);
弦线、子线;
int i=0;
while(i<获取行数(文件名)){
getline(流、线);
如果(是_标题(行)){
ifstream子_流(文件名);
getline(子_流,子行);
while(是_头(子行)){
}
}
}
返回_数据列表的编号_;
}

我无法获取数据列表的其余部分()我愿意接受任何建议。

我会逐行阅读:

std::vector<std::vector<double>> database;
//...
std::string text_line;
while (std::getline(file, text_line))
{
    // Ignore comment and blank lines.
    if ((text_line[0] == '#') || (text_line.empty()))
    {
        continue;
    }
    // Process the data in the text line.
    bool is_data_line = true;
    while (is_data_line)
    {
        std::istringstream record_stream(text_line);
        std::vector<double> data_row;
        double number;
        while (record_stream >> number)
        {
            data_row.push_back(number);
        }
        database.push_back(data_row);
        std::getline(file, text_line);
        if ((text_line[0] == '#') || (text_line.empty()))
        {
            break;
        }
    }
}
std::向量数据库;
//...
std::字符串文本_行;
while(std::getline(文件、文本行))
{
//忽略注释和空行。
if((text_line[0]='#')| |(text_line.empty()))
{
继续;
}
//处理文本行中的数据。
bool是_data_line=true;
while(是数据线)
{
std::istringstream记录流(文本行);
std::矢量数据行;
双数;
while(记录\u流>>编号)
{
数据行。推回(数字);
}
数据库。推回(数据行);
std::getline(文件、文本行);
if((text_line[0]='#')| |(text_line.empty()))
{
打破
}
}
}
上面的代码读取一组数据。

OP将需要修改上述代码以处理多组数据。

这里有一个简单的方法来解决这个问题,它永远不会失败。只要拿出一张白纸。用简单明了的英语写下简短的句子,这是一个循序渐进的过程。完成后。通常,我们不会在Stackoverflow上为其他人编写代码。我们总是把这样的问题交给你们的橡皮鸭。在你的橡皮鸭同意你提出的行动计划后,只需把你写下来的东西直接翻译成C++。任务完成了!一些建议,没有必要,也不是一个好主意,打开同一个文件三次来回答这个问题。您应该能够通过打开文件一次并对文件进行一次遍历来找到组的数量。注意,您有三种类型的行(不是两种)标题行、数据行和空行。解决这类问题的简单方法是将其视为最终状态机(finate state machine,FSM)。做一些阅读,并起草一个简单的FSM来计算答案。简单的代码编写就完成了。我已经用其他函数完成了。