gpt4 book ai didi

c - 写入文件错误

转载 作者:行者123 更新时间:2023-11-30 20:36:21 25 4
gpt4 key购买 nike

在我的代码中,我正在读取一个名为 input-temps.txt 的文本文件(我下载了该文件,并将其放置在我正在处理的项目的文件夹中),其中包含 25 个温度每天要读取的值。我的程序读取这些值并将它们保存到数组中。计算完最大值、最小值和平均值后,它将这些值以及每小时温度表写入一个输出文件,我将其命名为 output-temps.txt

我的代码的问题是,当它运行时找不到文件。我尝试过其他循环,但得到了相同的结果。我到底出了什么问题?

谢谢。

#include <stdio.h>
#include <stdlib.h>

//function prototype
void calc_results(int time[], int size);
void read_temps(int temp[]);

//set size of array as global value
#define SIZE 25

int main () {
//Declare temperature array with size 25 since we are going from 0 to 24
int i, temp[SIZE];

read_temps(temp);
//Temperature for the day of October 14, 2015
printf("Temperature conditions on October 14, 2015:\n");
printf("\nTime of day\tTemperature in degrees F\n\n");

for (i = 0; i < SIZE; i++)
printf( "%d \t\t%d\n",i,temp[i]);
//call calc_results(temp, SIZE);
calc_results(temp, SIZE);
//pause the program output on console until user enters a key
return 0;
}

/*The method read_temps that takes the input array temp
and prompt user to enter the name of the input file
"input.txt" and then reads the temperatures from the file
for 24 hours of day */
void read_temps(int temp[]) {

char fileName[50];
int temperature;
int counter=0;

printf("Enter input file name : ");
//prompt for file name
scanf("%s",fileName);
//open the input file
FILE *fp=fopen(fileName, "r");
//check if file exists or not
if (fp=fopen("results.dat","r")== NULL) {
printf("File could not be opened.\n");
//if not exit, close the program
exit(0);
}
//read temperatures from the file input.txt until end of file is encountered
while(fscanf(fp,"%d",&temperature)!=EOF) {
//store the values in the temp array
temp[counter]=temperature;
//increment the counter by one
counter++;
}
//close the input file stream fp
fclose(fp);
}

void calc_results(int temp[], int size) {
int i, min, max, sum = 0;
float avg;
min = temp[0];
max = temp[0];
//Loop that calculates min,max, sum of array
for (i = 0; i < size; i++) {
if (temp[i] < min) {
min = temp[i];
}
if (temp[i] > max) {
max = temp[i];
}
sum = sum + temp[i];
}
avg = (float) sum / size;
//open an external output file
FILE *fout=fopen("output.txt","w");
//Temperature for the day of October 14, 2015
fprintf(fout,"Temperature conditions on October 14, 2015:\n");
fprintf(fout,"\nTime of day\tTemperature in degrees F\n\n");
//write time of day and temperature
for (i = 0; i < SIZE; i++) {
fprintf( fout,"%d \t\t%d\n",i,temp[i]);
}
printf("\nMin Temperature for the day is : %d\n", min);
printf("Max Temperature for the day is :%d\n", max);
printf("Average Temperature for the day is : %f\n", avg);
//write min ,max and avg to the file "output.txt"
fprintf(fout,"\nMin Temperature for the day is : %d\n", min);
fprintf(fout,"Max Temperature for the day is :%d\n", max);
fprintf(fout,"Average Temperature for the day is : %f\n", avg);
//close the output file stream
fclose(fout);
}

最佳答案

要检查文件是否存在,您必须使用“access()”函数。首先包括:

#include <unistd.h>          // For "access()" function
#include <errno.h> // For "errno"
#include <string.h> // For "strerror()" function
// add your stuffs here ...
// .....
// .....
int status = access(filename, F_OK);
int saved_error = errno; // For errno include <errno.h>
if (status == -1) {
fprintf(stderr, "File Error: %s\n", strerror(saved_error));
// For "strerror" include <string.h>
exit(EXIT_FAILURE);
}
// Now here file will exist and can be opened as:
FILE *fptr = fopen(filename, "r");
saved_error = errno; // Read more about errno: "man errno" commend in linux
// check for error
if(fptr == NULL){
fprintf(stderr, "File error: %s\n", strerror(saved_error);
exit(EXIT_FAILURE);
}
// Now access you file using fptr...

关于c - 写入文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com