Debug Assertion Failed,Expression stream!= nullptr

时间:2016-08-05 02:03:15

标签: c visual-studio compiler-errors

使用Visual Studio IDE,用C语言编写。程序读取基本.txt文件,从华氏温度转换为摄氏温度,并写入新的.txt文件为什么不找到.txt文件?这是我的代码:

#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");

}

here's where the .txt file is

this is the error code i received

2 个答案:

答案 0 :(得分:2)

我将文件命名为“temperature.txt”并且已经是.txt文件,因此文件名实际上是“temperature.txt.txt”。小错误,大问题。谢谢你的帮助。

答案 1 :(得分:0)

运行的可执行文件位于Debug文件夹中(在本例中),因此temperature.txt需要位于该文件夹中,或者需要将"..\temperature.txt"或类似内容传递到fopen()