C++ 使用 fscanf 命令迴圈讀取 TXT每行的數值資料(不用陣列)
// 設定匯入的衛星角速度和角加速度資料
char parasfileomega[100];
int flag = 1;
printf("是否匯入預設的衛星模擬角速度及角加速度引數檔案?(1:Yes/0:No)\n");
cin >> flag;
if (flag == 1) {
strcpy_s(parasfileomega, "Test8192.txt");
}
if (!flag) {
printf("請輸入衛星模擬角速度和角加速度引數檔案的完整路徑(使用雙斜槓):\n");
cin >> parasfileomega;
}
cout << "開始迴圈計算..." << endl;
double t = 0.0;
double pomegax=1, pomegay=1, pomegaz=1, pangaccx=1, pangaccy=1, pangaccz=1;
FILE *loadOmegaxyz;
loadOmegaxyz = fopen(parasfileomega, "r");
cout << "迴圈之前"<<pomegax << " " << pomegay << " " << pomegaz << endl;
for (int i = 0; i < rounds; i++)
{
t = t + i * step;
fscanf(loadOmegaxyz, "%lf%lf%lf%lf%lf%lf\n", &pomegax, &pomegay, &pomegaz, &pangaccx, &pangaccy, &pangaccz);
cout << "迴圈內部"<<pomegax << " " << pomegay << " " << pomegaz << endl;
omega = Vector(pomegax, pomegay, pomegaz);//讀取輸入的引數;
angacc = Vector(pangaccx, pangaccy, pangaccz);//讀取輸入的引數;
}
fclose(loadOmegaxyz);
cout <<"出了迴圈"<< pomegax << " "<< pomegay<<" " << pomegaz << endl;
return 0;
}