2016-08-05 489 views
0

使用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"); 

}

here's where the .txt file is

this is the error code i received

+0

你不檢查'fopen'是否成功。看起來結果「fopen」失敗了。 – kaylum

+1

請不要將短信作爲圖片發佈。將其作爲文本粘貼到問題中,以便其他人可以更容易地將其複製以在評論/回答中引用。 – kaylum

+0

即使我檢查它是否成功打開,這也不會改變它爲什麼不成功。爲什麼找不到.txt文件? – Destreation

回答

1

我將文件命名爲「temperature.txt」,它已經是一個.txt文件,所以文件名實際上是「temperature.txt.txt」。小錯誤,大問題。謝謝你們的幫助。

+0

啊,你去了。 :)我可能已經注意到,從截圖。將其標記爲答案(並給出自己的一些觀點)。 –

+0

非常感謝大衛 – Destreation

0

運行在Debug文件夾(在這種情況下),所以temperature.txt需要可執行文件是文件夾中,或者它需要是"..\temperature.txt"或類似的傳入fopen()

+0

這是爲什麼?那是正確的語法正確? – Destreation

+0

我沒看到kaylum在看什麼,所以我無法幫助。但作爲另一種觀察,您在'Projects \ TempRecorder.c \ TempRecorder.c \'中顯示'temperature.txt',但在Projects \ TempRecorder.c \ Debug \中顯示可執行文件,因此'fopen()'的相對路徑在這種情況下實際上是'.. \ TempRecorder.c \ temperature.txt'。 –

+0

所以我必須把整個路徑從我的C盤開始或調試正確?因爲我試了兩次,仍然無法運行 – Destreation

相關問題