正在读取文本文件
Reading a textfile
我的文本文件如下所示:
1 41 -1 -1.492 -2.9555
1 42 -1 -1.49515 -2.9745
1 43 -1 -1.49799 -2.99361
1 44 -1 -1.50051 -3.01283
1 45 -1 -1.5027 -3.03213
1 46 -1 -1.50416 -3.05301
1 47 -1 -1.50556 -3.07248
(数字用空格分隔,而不是用制表符分隔。)
我想用C++编写一个程序,读取这些值并将它们放入向量中,但我该怎么做呢?
我试过这个:
while(!file.eof()){
scanf("%d %d %d %f %f", &x, &y, &z, &eta, &phi);
}
但它不起作用。
有人能告诉我为什么以及如何解决这个问题吗?
您使用的是C++,因此不要使用scanf
,而是使用std::ifstream
。
#include <fstream>
using namespace std;
ifstream file("input.txt");
int x, y, z;
float eta, phi;
// Read you file until the end
while( file >> x >> y >> z >> eta >> phi )
{
// Print the values
cout << "x : " << x << " y :" << y << " z : " << z << " eta : " << eta << " phi : " << phi << endl;
}
如Armen Tsirunyan所示,您也可以使用struct
来使用vector
存储数据。这取决于你想对你的数据做什么。
结构的优点在于,您有一个实体来表示具有所有数据的对齐。您可以重载operator>>
以读取具有更干净代码的文件。
代码将如下所示:
#include <fstream>
#include <vector>
using namespace std;
struct s_Data
{
int x, y, z;
float eta, phi;
};
istream& operator >> (istream& iIn, s_Data& iData)
{
return iIn >> iData.x >> iData.y >> iData.z >> iData.eta >> iData.phi;
}
ifstream file("input.txt");
// Read you file until the end
s_Data data;
vector<s_Data> datas;
while( file >> data )
{
// Print the values
cout << "x : " << data.x << " y :" << data.y << " z : " << data.z << " eta : " << data.eta << " phi : " << data.phi << endl;
// Store the values
datas.push_back( data );
}
这里s_Data
表示您的lign和您想要的5个值。vector<s_Data>
表示在文件中读取的所有值。你可以通过以下操作阅读:
unsigned int size = datas.size();
for ( unsigned int i = 0; i < size; i++ )
cout << datas[i].x; // read all the x values for example
您必须将它们放在一个char数组中,然后在此处使用atof
将它们转换为float。我通常使用fstream
库,并将它们作为char数组读取,然后将它们转换为float/int等。
#include <fstream>
struct MyData
{
int x, y, z;
double phi, eta;
};
vector<MyData> v;
ifstream fin("input.txt");
MyData d;
while(fin >> d.x >> d.y >> d.z >> d.phi >> d.eta)
{
v.push_back(d);
}
或者,您可以超载操作员>>并执行以下操作:
istream& operator >> (istream& in, MyData& d)
{
return in >> d.x >> d.y >> d.z >> d.phi >> d.eta;
}
int main()
{
ifstream fin ("input.txt");
vector<MyData> v;
v.assign(istream_iterator<MyData>(fin), istream_iterator<MyData>());
}
这将允许您拥有与文件中的行一样多的向量。如果你对此不感兴趣,你可以使用一个简单的std::vector<double>
,并使用std::copy
来推送你的值
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
#include <string>
std::ifstream file("file.txt");
std::istream &is = file; // thanks to polymorphism
std::string str;
std::vector<std::vector<double> > vects;
while ( str = getline(is) )
{
std::vector<double> tmpVect;
std::copy (std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>(),
std::back_inserter(tmpVect));
vects.push_back(tmpVect);
}
这个std::copy
将遍历整行,用它刚刚从行中获取的值在临时向量上调用push_back
。
编辑:看完你的评论后,我相信这实际上不是你所要求的,但在其他情况下也会有所帮助。请编辑您的原始帖子,使其更加清晰。
scanf在未读取任何内容时返回-1。你可以。
while(scanf()>=0)
{
}
相关文章:
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- C++程序不返回文件中文本和字符串数据的值
- 提升配置文件的文本文件接收器设置
- 随机访问文件格式,用于分层组织的二进制文件和文本文件
- 是否可以使头文件使文本居中?- 在控制台中
- 如何为包含其他 protobuf 的 protobuf 文件编写文本
- 使用strtok_s显示来自文本文件的文本
- 根据某些输入文件预测文本生成'random'下一个字符
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- 我怎么知道我是在读二进制文件还是文本文件
- 如何在wxWidgets的同一窗口中拖放文件和文本
- 二进制文件到文本文件,文本文件到二进制文件
- 无法从文件打印文本
- 设置新文件默认文本
- C++显示文本文件...("Echo"文本文件)
- 从Windows应用程序的文本框中抓取文本
- 音频文件到文本使用SAPI或同等功能的SR
- 对于大文件,文本文件中的大多数常用词从未完成
- 通过套接字发送文件和文本
- 从文件读取文本到无符号字符数组