简体   繁体   English

用C ++解析文件

[英]Parse a File with C++

I have created code that will execute a file and then will execute code and store that code into a .csv filer shown below 我创建了将执行文件的代码,然后将执行代码并将其存储到如下所示的.csv文件管理器中

#include <string>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace.std;

int main(){
    int foo = 10;
    for(int i =1; x<=8 x++){

      std::stringstream ss;
      ss << "echo " << foo << " | ./triad >> scaling.csv"<<'\n';
      std::cout << ss.str().c_str() <<std::endl;
      system(ss.str().c_str());

      foo=foo*10
    }
return 0;
}

The triad program is one that I am given and cannot change. 三合会程序是我得到的,不能更改。 I run basically 10 iterations on it and print the output of that to a scaling.csv, to give me the following output 我基本上在上面运行了10次迭代,并将其输出打印到scaling.csv,以便获得以下输出

Length:        10    MFLOP/s:    2541.29
Length:       100    MFLOP/s:    2515.85
Length:      1000    MFLOP/s:    3616.75

and so on... 等等...

does anyone know how to parse that file so instead my scaling.csv will look something like this 有谁知道如何解析该文件,所以我的scaling.csv看起来像这样

Key,Value
10,2541.29
100,2515.85
1000,3616.75

Again what gets printed out by triad I cannot change. 同样,三合会输出的内容我无法更改。

Simple: 简单:

int main()
{
     string s;
     int key; float val;
     ifstream out("out.txt");
     ofstream in("in.txt);

    in >> s;
    in >> key; // key == 10
    in >> s;
    in >> val; // val == 2541.29
}

Put this into a function and you have: 将其放入函数中,您将:

void extract(std::istream& in, int& key, float& val)
{
    // same as above
}

int main()
{
    int key; float val;
    while (in)
    {
        extract(in, key, val);
        out << key << ", " << val << std::endl;
    }
}

But we're performing the input after we check the stream. 但是我们在检查流之后执行输入。 This can cause problems. 这可能会引起问题。 Let's have the extract function return a reference to the stream: 让我们让extract函数返回对流的引用:

std::ios& extract(std::istream& in, int& key, float& val)
{
    // same as above
}

int main()
{
    int key; float val;
    while (extract(in, key, val))
//         ^^^^^^^^^^^^^^^^^^^^^
    {
        out << key << ", " << val << std::endl;
    }
}

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

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