我遇到了一些问题fscanf
。我对 C 很陌生,但我似乎无法fscanf
从 .txt 文件中加载正确的信息。
int main() {
//Vars
FILE *tempFileIn;
int rowIndex = 0;
int objectIdNum;
double magnitudePhotographed;
double distance;
int velocity;
double magnitudeCorrected;
double magnitudeTotal;
//Read File Data
tempFileIn = fopen("hubbleData.txt","r");
if (tempFileIn == NULL) {
printf("File read error.");
}
printHeaders();
while(!feof(tempFileIn)) {
fscanf(tempFileIn, "%lf %lf %lf %lf %lf", &objectIdNum, &distance, &velocity, &magnitudeCorrected, &magnitudeTotal);
printf( "%2.3f %2.3f", velocity, distance);
printf("\n");
rowIndex++;
}
return 0;
}
速度按预期打印,但距离始终打印为 0.0000。如果我切换两者的打印顺序,距离将正确打印,速度将打印为 0.0000。我只关心第二列和第三列,但必须按照项目指南扫描所有列。
Input format:
1 0.032 +170 1.5 -16.0
2 0.034 +290 0.5 17.2
6822 0.214 -130 9.0 12.7
598 0.263 -70 7.0 15.1
221 0.275 -185 8.8 13.4
224 0.275 -220 5.0 17.2
5457 0.45 +200 9.9 13.3
Actual Output:
170.000 0.000
290.000 0.000
-130.000 0.000
-70.000 0.000
-185.000 0.000
-220.000 0.000
200.000 0.000
Expected Output:
170.000 0.032
290.000 0.034
-130.000 0.214
-70.000 0.263
-185.000 0.275
-220.000 0.275
200.000 0.45