从文本文件C++中读取

smi*_*dha 0 c++

我有一个包含三列数字的文本文件; 每列一列用于x,y,z一堆点的坐标.所有数字都在0和之间1.

我创建了以下结构:

typedef struct 
{
    double xcd, ycd, zcd;
} point; 
Run Code Online (Sandbox Code Playgroud)

我想创建一个size-N类型的结构数组point.然后我想通过扫描行的文本文件,行,为n个粒子,我想提出三个号码上n条线上到相应的xcd,ycd和zcd位置.

告诉我是否有一些有效的方法来解决这个问题.

Set*_*gie 6

只要做到像之前已经显示500万十亿kajillion次,使用ifstream,vector以及其他各种饰物.

ifstream infile("myfile.txt");

// check if file opened successfully
if (!infile) {
    cerr << "failure, can't open file" << endl;
    cin.get();
    return EXIT_FAILURE;
}

// the container in which we will store all the points
vector<point> points;

// a temporary point to hold the three coords in while we read them all
point tmp;

// read in three doubles from infile into the three coords in tmp
while (infile >> tmp.xcd && infile >> tmp.ycd && infile >> tmp.zcd)
    // add a copy of tmp to points
    points.push_back(tmp);
Run Code Online (Sandbox Code Playgroud)

这将在三个双打阅读并把它们放在一个point然后把那一份point的points.但是,如果文件模数3中的数字数不为0,则它​​将停止并且不会将不完整的点添加到points.