2013-05-28 43 views
0

我从文件中添加一个double值到一个变量,并将其推入一个向量中,格式为“338620.3478”,然后从该向量获得值,它只获得“338620”,因为它无法获得所有的双重价值。获取完整的双倍值

那么我怎样才能得到像原始格式的完整双值?

的代码:

struct Point { 
    double x, y; 

    bool operator <(const Point &p) const { 
      return x < p.x || (x == p.x && y < p.y); 
    } 

};

ifstream iFile("griddata.dat"); //read a file (grid) 
string line; 
Point Grid;/
while(getline(iFile,line)) 
{ 
    unsigned pos = line.find(",");//the symbol is used to separate X and Y 
    std::string strs = line.substr(0,pos); // get X 
    std::string strs2 = line.substr(pos+1); // get Y 

    Grid.x = atof(strs.c_str()); // get the first cooordinate X 
    Grid.y = atof(strs2.c_str()); // get the second cooordinate Y 

    // A list of coordinates of grid is stored into the vector gridPoints 
    gridPoints.push_back(Grid); // adding the points of grid to vector 

} 
int j; 

for(j=0;j<gridPoints.size();j++) 
{ 
    //here i cannot get the full double value for gridPoints[j].x; 
    //....it just gets "338620" 

} 

文件的格式(griddata.dat):

338620.3478,6196150.566

谢谢!

+1

请出示一些代码... –

+0

能否请您提供更多相同的细节。你可以在这里发布你的代码,这样很容易解决问题。 :) –

+0

你应该显示你的代码。否则,我们只能猜测什么是错的。 – crashmstr

回答

0

假设你的Point类是在Windows框架中,我很确定它的成员是int类型。

无论哪种方式,您的值都被转换为非浮点类型并被截断的类型。

+0

我将x和y声明为double值,您可以在上面看到,我刚刚添加了! – bluewonder

+1

@ user2076858 - 那么一旦你得到它们,你想要做什么?代码在哪里?你怎么知道这些值是错的? – Aesthete

+0

尽管当我使用cout << std :: setprecision(15)<< gridPoints [j] .x时,它正确显示 – bluewonder