我有以下代码,我正在使用它来重新组织我正在进行的模拟中的一些数据。本质上,我读入一个组件列表,然后将它们重新组织成张量,张量的索引对应于空间位置。
我第一次读到文件时,只计算行数
int k1 = -1;
ifstream fin;
fin.open("C:\\Users\\Chelsea\\Documents\\Visual Studio 2015\\Projects\\Full Spin Tracking\\Full Spin Tracking\\Fields\\RoundField1cmGrid.txt"); // open a file
if (!fin.good())
return 1; // exit if file not found
//Count lines in file
while (fin.good()) {
getline(fin, line);
k1++;
if (k1 >= 270005) { break; }
}
fin.clear();
fin.seekg(0, fin.beg); // reset the position of the file to the beginning.
cout << k1 << endl;
然后我设置数组来保存数据,并为这些数组分配数据
//Initialize new arrays to hold static field info
double *xx = new (nothrow) double[k1];
double *yy = new (nothrow) double[k1];
double *zz = new (nothrow) double[k1];
double *bbx = new (nothrow) double[k1];
double *bby = new (nothrow) double[k1];
double *bbz = new (nothrow) double[k1];
double *bbmag = new (nothrow) double[k1];
//Write file static field data to 1D arrays
while (fin.good()) {
data = line.find('\t');
x1 = stof(line.substr(0, data));
xx[j1] = x1;
data2 = line.find('\t', data + 1);
y1 = stof(line.substr(data + 1, data2));
yy[j1] = y1;
data3 = line.find('\t', data2 + 1);
z1 = stof(line.substr(data2 + 1, data3));
zz[j1] = z1;
data4 = line.find('\t', data3 + 1);
bx1 = stof(line.substr(data3 + 1, data4));
bbx[j1] = bx1;
data5 = line.find('\t', data4 + 1);
by1 = stof(line.substr(data4 + 1, data5));
bby[j1] = by1;
data6 = line.find('\t', data5 + 1);
bz1 = stof(line.substr(data5 + 1, data6));
bbz[j1] = bz1;
bmag = stof(line.substr(data6 + 1));
bbmag[j1] = bmag;
j1++;
if (j1 == k1) { break; }
}//end while
fin.close();//Close file
现在,虽然我再也没有使用整数k1,但我发现如果我对变量进行监视,k1的值会在文件关闭之前发生变化,我无法弄清楚原因,但是我我怀疑其他东西是错误的,我想确保这不会搞砸我在其他任何地方的程序。
导致此值发生变化的原因是什么?