[英]Parse a File with C++
我创建了将执行文件的代码,然后将执行代码并将其存储到如下所示的.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;
}
三合会程序是我得到的,不能更改。 我基本上在上面运行了10次迭代,并将其输出打印到scaling.csv,以便获得以下输出
Length: 10 MFLOP/s: 2541.29
Length: 100 MFLOP/s: 2515.85
Length: 1000 MFLOP/s: 3616.75
等等...
有谁知道如何解析该文件,所以我的scaling.csv看起来像这样
Key,Value
10,2541.29
100,2515.85
1000,3616.75
同样,三合会输出的内容我无法更改。
简单:
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
}
将其放入函数中,您将:
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;
}
}
但是我们在检查流之后执行输入。 这可能会引起问题。 让我们让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.