阅读文本文件

时间:2013-07-05 08:23:28

标签: c++

我的文本文件如下所示:

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

但它不起作用。

有人可以告诉我为什么以及如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

您使用的是 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。这取决于您想要对数据做什么。

结构的优点是你有一个实体代表一个包含所有数据的lign。您可以重载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

答案 1 :(得分:0)

您必须将它们放在char数组中,然后使用atof here将它们转换为float。我通常使用fstream库,我将它们作为char数组读取,然后将它们转换为float / int等。

答案 2 :(得分:0)

这将允许您拥有与文件中的行一样多的向量。如果您对此不感兴趣,可以使用简单的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,并使用刚刚从该行抓取的值。

编辑: 阅读你的评论后,我相信这实际上不是你要求的,但在其他情况下它可能会有所帮助。请编辑您的原始帖子以便更清楚。

答案 3 :(得分:0)

#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);
}

或者,您可以重载运算符&gt;&gt;并这样做:

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>());
}

答案 4 :(得分:-1)

当没有读取任何内容时,scanf返回-1。你可以做到。

while(scanf()>=0)
{

}
相关问题