我有一个文件,前几行看起来像这样:
1 436.514 0.587 8.318 1 3 8 0.929 7 0.972 2 1.440
2 436.004 0.744 7.020 1 3 10 1.117 9 1.155 1 1.440
3 436.263 0.603 5.029 2 1 9 0.916
4 437.966 0.594 6.086 2 1 9 0.835
5 434.577 1.454 5.820 2 1 10 0.898
6 433.990 1.139 7.596 2 1 10 0.919
7 437.917 0.102 8.485 4 3 1 0.972 11 1.503 12 1.428
8 435.617 0.849 9.510 4 3 13 1.463 1 0.929 14 1.490
9 436.839 0.691 5.880 4 3 4 0.835 3 0.916 2 1.155
10 434.623 1.036 6.798 4 3 6 0.919 5 0.898 2 1.117
11 438.321 39.569 9.683 3 1 7 1.503
12 438.614 39.463 7.420 3 1 7 1.428
13 434.384 1.154 9.304 3 1 8 1.463
Run Code Online (Sandbox Code Playgroud)
问题是我无法转换这些值,这些值在称为line的变量中被读取为字符串,然后存储到由空格分隔的单独char数组中.但我的问题是我无法使用std C++函数将这些值转换为适当的类型.
我的意思是我不控制文件中各个位置的值,因此我无法提供预定义函数将其转换为该类型.
例如:在第一行中,第一个值是int,后跟3个浮点数,然后是int,依此类推.而且每一行中的值的数量也不是恒定的.因此我无法将它们转换为所需的类型.我试过lexical_cast,如果它们与预定义类型不同,则无法转换值.
例如:如果我让我们说
str = "123";
float num = lexical_cast<float>(str)
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误.对于只能转换具有该类型值的字符串的函数stof或stod函数也是如此.
这是我现在的代码:
while (i < line.length()){
if (line[i] != ' '){
a[j++] = line[i++];
if (i == line.length()){
a[j] = '\0';
------->int num = std::stoi(a);
std::cout << num << " ";
}
}
else{
a[j] = '\0'; j = 0;
----------->float num = std::stof(a);
std::cout << num << " ";
while (line[i] == ' ') i++;
}
}
Run Code Online (Sandbox Code Playgroud)
标有箭头的地方是问题区域.我可以通过任何方式将这些值轻松地读入适当类型的适当变量中.
注意:我不能手动插入每个变量,并考虑其类型,因为行数是100000.所以这是不可能的.
我只有几分钟的时间,但这里有一个合适的代码大纲,它假设您希望将所有数据同时保存在内存中以进行某些跨线分析:
struct Data
{
int a;
float b, c, d;
int e, f;
std::vector<std::pair<int, float>> g;
};
int main()
{
int line_num = 0;
std::vector<Data> all_data;
if (std::ifstream in(filename))
{
std::string line;
while (getline(in, line))
{
++line_num;
std::istringstream iss(line);
Data data;
if (iss >> data.a >> data.b >> data.c >> data.d
>> data.e >> data.f)
{
int i; float f;
while (iss >> i >> f)
data.g.push_back(std::make_pair(i, f));
all_data.push_back(data);
}
else
std::cerr << "unable to parse mandatory fields "
"from line #" << line_num << " '" << line
<< "', ignoring and continuing...\n";
}
... use all_data for whatever analysis you want...
}
else
std::cerr << "unable to open file\n";
}
Run Code Online (Sandbox Code Playgroud)
笔记
istringstream来解析值int/ float值对我希望现在这对你来说太过分了.
如果你使用一个知道如何向你投射AST数据类型的解析器生成器,仍然希望向公众展示优雅代码的优雅程度.这是一个Spirit示例,可以自动处理所有转换到以下vector<line_rec>位置line_rec:
struct line_record {
int a;
float b, c, d;
int e;
// column f is rest.size()
std::vector<std::pair<int, float> > rest;
};
Run Code Online (Sandbox Code Playgroud)
#include <boost/fusion/include/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
struct line_record {
int a;
float b, c, d;
int e;
// column f is rest.size()
std::vector<std::pair<int, float> > rest;
};
BOOST_FUSION_ADAPT_STRUCT(line_record, a, b, c, d, e/*, f*/, rest)
// define a Spirit Grammar
using Iterator = boost::spirit::istream_iterator;
namespace parser {
static auto const line = [] {
using namespace boost::spirit::qi;
_a_type number_of_pairs;
rule<Iterator, line_record(), locals<unsigned> > line_parser;
return line_parser %= skip(blank) [
int_ >> float_ >> float_ >> float_ >>
int_ >> omit[ uint_ [ number_of_pairs = _1 ] ] >>
repeat(number_of_pairs) [ int_ >> float_ ] >>
(eol|eoi)
];
}();
}
#include <fstream>
int main() {
using namespace std;
ifstream ifs("input.txt");
Iterator first(ifs >> noskipws), last;
vector<line_record> all_data;
if (parse(first, last, *parser::line, all_data))
{
cout << "Parsed " << all_data.size() << " lines\n";
for (auto& rec : all_data) {
cout << rec.a << "\t"
<< rec.b << "\t" << rec.c << "\t" << rec.d << "\t"
<< rec.e << "\t"
<< rec.rest.size();
for (auto& trailing : rec.rest)
cout << "\t(" << trailing.first << ", " << trailing.second << ")";
cout << "\n";
}
} else {
cout << "Parse failed\n";
}
if (first != last) {
cout << "Remaining input: '" << string(first, last) << "'\n";
}
}
Run Code Online (Sandbox Code Playgroud)
根据您的问题输入:
Parsed 13 lines
1 436.514 0.587 8.318 1 3 (8, 0.929) (7, 0.972) (2, 1.44)
2 436.004 0.744 7.02 1 3 (10, 1.117) (9, 1.155) (1, 1.44)
3 436.263 0.603 5.029 2 1 (9, 0.916)
4 437.966 0.594 6.086 2 1 (9, 0.835)
5 434.577 1.454 5.82 2 1 (10, 0.898)
6 433.99 1.139 7.596 2 1 (10, 0.919)
7 437.917 0.102 8.485 4 3 (1, 0.972) (11, 1.503) (12, 1.428)
8 435.617 0.849 9.51 4 3 (13, 1.463) (1, 0.929) (14, 1.49)
9 436.839 0.691 5.88 4 3 (4, 0.835) (3, 0.916) (2, 1.155)
10 434.623 1.036 6.798 4 3 (6, 0.919) (5, 0.898) (2, 1.117)
11 438.321 39.569 9.683 3 1 (7, 1.503)
12 438.614 39.463 7.42 3 1 (7, 1.428)
13 434.384 1.154 9.304 3 1 (8, 1.463)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |