正在读取文本文件

Reading a textfile

本文关键字:文件 取文本 读取      更新时间:2023-10-16

我的文本文件如下所示:

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)
{
}