C++ 从文本文件中提取不均匀数据
我有一个数据文件,每行包含九个不同变量的值:C++ 从文本文件中提取不均匀数据,c++,string,file,C++,String,File,我有一个数据文件,每行包含九个不同变量的值:x,y,z,v_x,v_y,v_z,m,ID,v。我正在编写一个程序,从数据文件中提取x、y和z值。我对这种类型的过程比较陌生,在这样做时遇到了问题,因为值的长度并不总是相同的。数据文件的一部分示例如下(仅x,y,z列): 请注意,在大多数情况下,每个数字的长度是11个空格,但情况并非总是如此。我编写的代码如下: #include <cmath> #include <cstdlib> #include <fstream&g
x
,y
,z
,v_x
,v_y
,v_z
,m
,ID
,v
。我正在编写一个程序,从数据文件中提取x
、y
和z
值。我对这种类型的过程比较陌生,在这样做时遇到了问题,因为值的长度并不总是相同的。数据文件的一部分示例如下(仅x
,y
,z
列):
请注意,在大多数情况下,每个数字的长度是11个空格,但情况并非总是如此。我编写的代码如下:
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// data created by Gadget2
const string gadget_data("particles_64cubed.txt");
int main()
{
cout << "GADGET2: Extracting Desired Data From ASCII File." << endl;
// declaring vectors to store the data
int bins = 135000000; // 512^3 particles = 134,217,728 particles
vector<double> x(bins), y(bins), z(bins);
// read the data file
ifstream data_file(gadget_data.c_str());
if (data_file.fail())
{
cerr << "Cannot open " << gadget_data << endl;
exit(EXIT_FAILURE);
}
else
cout << "Reading data file: " << gadget_data << endl;
string line;
int particles = 0;
while (getline(data_file, line))
{
string x_pos = line.substr(0, 11);
double x_val = atof(x_pos.c_str()); // atof converts string to double
string y_pos = line.substr(12, 11);
double y_val = atof(y_pos.c_str());
string z_pos = line.substr(24, 11);
double z_val = atof(z_pos.c_str());
if (particles < bins)
{
x[particles] = x_val;
y[particles] = y_val;
z[particles] = z_val;
++particles;
}
}
data_file.close();
cout << "Stored " << particles << " particles in positions_64.dat" << endl;
vector<double> x_values, y_values, z_values;
for (int i = 0; i < particles; i++)
{
x_values.push_back(x[i]);
y_values.push_back(y[i]);
z_values.push_back(z[i]);
}
// write desired data to file
ofstream new_file("positions_64.dat");
for (int i = 0; i < x_values.size(); i++)
new_file << x_values[i] << '\t' << y_values[i] << '\t' << z_values[i] << endl;
new_file.close();
cout << "Wrote desired data to file: " << "positions_64.dat" << endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//Gadget2创建的数据
常量字符串小工具数据(“particles_64cubed.txt”);
int main()
{
cout我注意到您已经在使用ifstream
和getline
读取文件了。为什么您要将行切割成N个字符的块并atof
?我的意思是,iostreams可以读写成整数、双精度等,在cin
和cout
的示例中可以看到这一点
有一个istringstream
类可以轻松帮助您:
std::istringstream input(line); // line is std::string from getline()
double x,y,z;
if(input >> x >> y >> z) // just this! and it's already a simple error check
; // do something with x,y,z
else
; // handle the error
它应该可以正常工作,因为您已经进行了逐行读取,并且数据由空格分隔,默认情况下,>
操作符会忽略这些空格
仅供参考:您可以找到分隔符,然后根据该分隔符而不是固定数字对其进行子串。请检查该功能,它提供了一个根据用户提供的分隔符拆分字符串的功能。非常感谢,这完成了任务!唯一的问题是,我丢失了原始数据中的一些有效数字。生成的数据文件有两个字符imal最多只能放七位。有什么想法吗?@LeighK:我不记得运算符
有任何问题。它应该按顺序读取每个数字,并尝试将其放入双精度中,因此您应该只观察双精度本身造成的精度损失。我想这可能是因为运算符的某些默认精度设置>
std::istringstream input(line); // line is std::string from getline()
double x,y,z;
if(input >> x >> y >> z) // just this! and it's already a simple error check
; // do something with x,y,z
else
; // handle the error