使用C ++在文件中剪切和重组数字

问题描述 投票:0回答:2

我有以下代码

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
#include<fstream>
#include<iomanip>
#include<cstdio>



int main() {

//Here the code that creates the file benchmarks_cleaned.dat

int ncol = 16; //number of data per row
double datos[ncol];
char aux2;
double aux3;
int icol;

ifstream benchmarks2;
ofstream out_benchmarks2;

benchmarks2.open("benchmarks_cleaned.dat");
out_benchmarks2.open("benchmarks_final.dat");

if (benchmarks2.is_open()) {//second if

  for (icol = 0; icol < ncol; icol++) {
    benchmarks2>>datos[icol];
    out_benchmarks2<<datos[icol]<<" ";
  };

  out_benchmarks2<<"\n";
  benchmarks2.get(aux2);

  while (aux2 != 'X') {
    benchmarks2.unget();
    benchmarks2>>aux3;

    if (aux3 != datos[0]) {
      benchmarks2.get(aux2);
    } else {
      out_benchmarks2<<datos[0]<<" ";
      for (icol = 1; icol < ncol; icol++) {
        benchmarks2>>datos[icol];
        out_benchmarks2<<datos[icol]<<" ";
      };

      out_benchmarks2<<"\n";
      benchmarks2.get(aux2);

    };

  };

} else {
  cout<<"ERROR: unable to open the file"<<endl;
};//end of second if

out_benchmarks2<<"X";

out_benchmarks2.close();

out_benchmarks2.close();
benchmarks2.close();


return 0;
}; //end of main

数据文件dummyValues.dat为:

{5.12, 0.1, 0.25}   {{0.10, 4, 3, 2, 1, 1.44, 10.2}}       {11.1, 12.2, 13.3, 14.4, 15.5, 16.6} 1000 2000 {{{{ 5.12, 0.1} {17.7, 18.08, 19.0, 020.0}   {1.115, 2.0, 3.01, 4.65, 5, 6, 7, 8, 9, 10.0}, 3000 4000    { 5.12, 0.1} {117.7, 118.08, 119.0, 0120.0}   {11.115, 21.0, 31.01, 41.65, 51, 61, 71, 81, 91, 110.0} 5000 6000       X

在benchmarks_cleaned.dat中,将该文件缩小为仅用空格分隔的数字。现在的想法是编写Benchmarks_final.dat,其中每行中只有16个值,并且它们必须以相同的数字5.12 = datos[0]开头,并且您可以检查的数字与dummyValues.dat重复

尽管如此,确实确实按需创建了Benchmarks_cleaned.dat(请参阅下文),但是从未启动Benchmarks_final.dat。我检查了程序是否运行,但没有在Benchmarks_final.dat中编写任何内容。错误在哪里?

benchmarks_cleaned.dat是:

5.12 0.1 0.25 0.1 4 3 2 1 1.44 10.2 11.1 12.2 13.3 14.4 15.5 16.6 1000 2000 5.12 0.1 17.7 18.08 19 20 1.115 2 3.01 4.65 5 6 7 8 9 10 3000 4000 5.12 0.1 117.7 118.08 119 120 11.115 21 31.01 41.65 51 61 71 81 91 110 5000 6000 X
c++ arrays dataframe stream
2个回答
1
投票

您对get(...)的呼叫捕获空格,并且缺少'X'。

不需要get。摆脱aux2并将while循环更改为:

while(benchmarks2 >> aux3)

0
投票

因此,给出了答案。很好,正确。已接受并接受。很好。

仅作为附加信息,我将展示使用更现代的语言元素的C ++解决方案。该解决方案跳过了中间文件(可以由一个文件名生成),也不需要使用“ X”(也可以非常简单地添加)。

使用STL,我们可以用净的5行代码提供从原始源文件到最终目标文件的解决方案。

请参阅:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <iterator>
#include <algorithm>

std::regex reFloat{ R"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)" };

using SVector = std::vector<std::string>;
using SVectorIter = SVector::iterator;

int main() {
    // Open source file and check, if it coud be opened
    if (std::ifstream sourceFileStream{ "r:\\dummyValues.dat" }; sourceFileStream) {

        // Open destination file and check, if it could be opened
        if (std::ofstream finalFileStream("r:\\benchmarks_final.dat"); finalFileStream) {

            // Algorithm start ----------------------------------------------------------
            // Define a string variable and initialize it with the contents of the file
            std::string completeFile(std::istreambuf_iterator<char>(sourceFileStream), {});

            // Define vector and initialize it with all float values from the file
            SVector values(std::sregex_token_iterator(completeFile.begin(), completeFile.end(), reFloat), {});

            // Iterate over the vector and find the next value equal to first-value
            for (SVectorIter svi{ values.begin() }; (svi = std::find(svi, values.end(), values[0])) != values.end(); ++svi) {

                 // Copy 16 value to the final file
                std::copy_n(svi, std::min(16, std::distance(svi, values.end())), std::ostream_iterator<std::string>(finalFileStream, " "));
                finalFileStream << '\n';
            }
            // Algorithm end ----------------------------------------------------------
        }
        else {
            std::cerr << "\n*** Error: Could not open final file\n";
        }
    }
    else {
        std::cerr << "\n*** Error: Could not open source file\n";
    }
}

我想不是,但是如果您有兴趣了解这一点,那么我会向您解释。请问,我很乐意提供帮助-

© www.soinside.com 2019 - 2024. All rights reserved.