2016-08-05 3 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

ああ、そこに行く。 :)私はスクリーンショットから気づいたかもしれません。これを答えにして(そしてあなたにいくつかのポイントを与える)。 –

+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

関連する問題