2013-07-05 90 views
1

我的文本文件看起来是这样的:读一个文本文件

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

但它不工作。

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

+0

avector中的每一行或向量中的每个数字? – Vijay

+1

不要在C++中使用'scanf',你有'std :: cin'。 – nouney

+0

你说你的尝试不行,但是出了什么问题?匆匆一瞥表明它应该正确读取值,那么它将它们存储在你失败的向量中? – Jules

回答

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

如图所示阿尔钦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代表你河旁你想要的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 
+0

非常感谢!!我已经解决。 如果我不想使用struct和我声明5个不同的向量,有什么区别? – Lunatica

+0

在这种情况下使用结构是一个更好的做法。我想你的'''','''''''''''''''每行属于同一个实体还是微积分权?在这种情况下,最好将它们存储在同一个实体('struct')中。你能否标记答案被接受? –

0

你必须把它们放在一个字符数组中,然后使用atofhere将它们转换为浮点数。我通常使用fstream库,我将它们读为char数组,然后将它们转换为float/int等。

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它只是从线上抓起来的。

编辑: 阅读您的评论之后,我认为这其实不是你问什么,但它可以在其他情况下很有帮助。请编辑您的原始帖子以便更清楚。

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

或者,你可以重载operator >>和做到这一点:在读什么

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

我对wrousseau说过的话也适合你。请不要发布代码只回答,特别是如果没有一个单一的评论。 – stefan

+0

Armen Tsirunyan通过这种方式,我在哪里阅读我的文件的所有行? – Lunatica

+0

@Lunatica:你读到你的载体v –

-1

scanf函数返回-1。你可以做。

while(scanf()>=0) 
{ 

}