2016-08-05 2 views
0

Visual Studio IDE를 사용하여 C. 프로그램으로 작성하면 화씨에서 섭씨로 변환하고 새로운 .txt 파일로 쓰는 기본 .txt 파일로 읽는 이유는 무엇입니까? txt 파일? 여기 내 코드입니다 :디버그 어설 션 오류, 식 스트림! = 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

Ahh, 거기에 간다. :) 나는 스크린 샷에서 그 사실을 알아 차렸을 것입니다. 이것을 답변으로 표시하십시오 (그리고 자신에게 몇 가지 포인트를주십시오). –

+0

대단한 데 대해 감사합니다. – Destreation

0

는 (이 경우) 디버그 폴더에 너무 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