使用Visual Studio IDE編寫C程序正在讀取一個基本的.txt文件,從華氏轉換爲攝氏度並寫入新的.txt文件爲什麼不能找到它。 txt文件?這裏是我的代碼:Debug Assertion Failed,Expression stream!= nullptr
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
double tempArray[30];
double temp;
double sum = 0;
int input_status;
int count = 0;
double convert;
FILE *infile, *outfile;
infile = fopen("temperature.txt", "r");
if (infile == NULL) {
perror("Failed: ");
return 1;
}
outfile = fopen("results.txt", "w");
int i = 0;
input_status = fscanf(infile, "%lf", &temp);
double max, min = temp;
while (input_status != EOF)
{
tempArray[i] = (temp - 32) * 5/9; ;
sum += tempArray[i];
fprintf(outfile, "%f \n", tempArray[i]);
if (tempArray[i] > max)
max = tempArray[i];
if (tempArray[i] < min)
min = tempArray[i];
count++;
i++;
input_status = fscanf(infile, "%lf", &temp);
}
double average = sum/count;
fprintf(outfile, "\n The average of the temperatures is %f \n", average);
fprintf(outfile, "\n The maximum of the temperatures is %f \n", max);
fprintf(outfile, "\n The minimum of the temperatures is %f \n", min);
fclose(infile);
fclose(outfile);
system("pause");
}
this is the error code i received
你不檢查'fopen'是否成功。看起來結果「fopen」失敗了。 – kaylum
請不要將短信作爲圖片發佈。將其作爲文本粘貼到問題中,以便其他人可以更容易地將其複製以在評論/回答中引用。 – kaylum
即使我檢查它是否成功打開,這也不會改變它爲什麼不成功。爲什麼找不到.txt文件? – Destreation