C请求用户txt文件,保存在数组中,并以txt输出
所以我已经建立了一个函数,计算了25个随机温度,并输出它们,有一个最大值、最小值和平均值。我现在正试图通过txt合并输入文件和输出文件 我试着做一些研究,尽我所能(即使我几乎不懂),有人能帮我解释一下代码吗C请求用户txt文件,保存在数组中,并以txt输出,c,arrays,file,text,C,Arrays,File,Text,所以我已经建立了一个函数,计算了25个随机温度,并输出它们,有一个最大值、最小值和平均值。我现在正试图通过txt合并输入文件和输出文件 我试着做一些研究,尽我所能(即使我几乎不懂),有人能帮我解释一下代码吗 int get_value(void); void calc_results(void); void read_temps(void); int sum = 0; int min = 0; int max = 0; int temp[25]; int i = 0; //For array
int get_value(void);
void calc_results(void);
void read_temps(void);
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0; //For array loop
int j = 0; //For printf loop
float avg = 0;
void read_temps() {
char fname[128];
printf("Enter .txt file name \n");
scanf("%123s", fname);
strcat(fname, ".txt");
FILE *inputf;
inputf=fopen(fname, "w");
for (i = 0; i < 25; i++){
temp[i] = fname;
sum += temp[i];
}
}
int main () {
calc_results();
return 0;
};
void calc_results(void) {
FILE * fp;
fp = fopen("Output_temps.txt", "w+");
avg = ((sum)/(25));
max = temp[0];
for(i=1;i<25;i++){
if(max<temp[i])
max=temp[i];
};
min =temp[0];
for(i=1;i<25;i++){
if(min>temp[i])
min=temp[i];
};
fprintf("Temperature Conditions on October 9, 2015 : \n");
fprintf("Time of day Temperature in degrees F \n");
for(j=0;j<25;j++){
fprintf(" %d %d\n",j,temp[j]);
}
fprintf("Maximum Temperature for the day: %d Degrees F\n", max);
fprintf("Minimum Temperature for the day: %d Degrees F\n", min);
fprintf("Average Temperature for the day: %.1f Degrees F\n", avg);
fclose(fp);
};
int get_值(void);
无效计算结果(无效);
无效读取时间(无效);
整数和=0;
int min=0;
int max=0;
内部温度[25];
int i=0//用于阵列循环
int j=0//用于printf循环
浮动平均值=0;
无效读_temps(){
char-fname[128];
printf(“输入.txt文件名\n”);
scanf(“%123s”,fname);
strcat(fname,“.txt”);
文件*inputf;
inputf=fopen(fname,“w”);
对于(i=0;i<25;i++){
温度[i]=fname;
总和+=温度[i];
}
}
int main(){
计算结果();
返回0;
};
作废计算结果(作废){
文件*fp;
fp=fopen(“Output_temps.txt”,“w+”);
平均值=((总和)/(25));
最大值=温度[0];
对于(i=1;i您的代码中有一些错误,最关键的是它没有编译。如果您遇到问题,您将希望按照说明进行操作。这样您会得到更好的响应。然后清楚地解释您遇到的具体问题,以及发生了什么或没有发生什么,而不是什么你没料到
在代码中,您似乎将temp
数组赋值为fname
变量,而不是从用户文件中读取int
数据
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// assuming you want a maximum of temperatures of 25
#define TEMP_NUMBER 25
// use a struct to maintain and the temparatur data in one place
// without resorting to using global variables or having functions
// that require numerous parameters.
struct temp_data {
char fname[128];
int max;
int min;
int sum;
int temps[TEMP_NUMBER];
float avg;
};
struct temp_data *temps_init(void)
{
// creates a pointer to struct temp_data to hold
// your various temparture related fields
struct temp_data *td = malloc(sizeof *td);
td->sum = 0;
td->avg = 0.0;
return td;
}
void read_temps(struct temp_data *td)
{
// in your sample code you have this set to "w", needs to be "r"
FILE *inputf = fopen(td->fname, "r");
// handle error
if (!inputf) {
perror("fopen");
exit(0);
}
for (int i = 0; i < TEMP_NUMBER; i++) {
// you were setting fname to the temparature array
// instead you need to scan the temp file
fscanf(inputf, "%d", &(td->temps[i]));
td->sum += td->temps[i];
}
}
void print_results(struct temp_data *td)
{
// a print function to separate logic
FILE *fp = fopen("Output_temps.txt", "w+");
if (!fp) {
perror("fopen");
exit(0);
}
fprintf(fp, "Temperature Conditions on October 9, 2015 : \n");
fprintf(fp, "Time of day Temperature in degrees F \n");
for(int i=0; i < TEMP_NUMBER; i++)
fprintf(fp, " %d %d\n", i, td->temps[i]);
fprintf(fp, "Maximum Temperature for the day: %d Degrees F\n", td->max);
fprintf(fp, "Minimum Temperature for the day: %d Degrees F\n", td->min);
fprintf(fp, "Average Temperature for the day: %.1f Degrees F\n", td->avg);
fclose(fp);
}
void calc_results(struct temp_data *td)
{
// do only calculations
td->avg = td->sum / TEMP_NUMBER;
td->min = td->temps[0];
td->max = td->temps[0];
for (int i=1; i < TEMP_NUMBER; i++) {
td->min = td->temps[i] < td->min ? td->temps[i] : td->min;
td->max = td->temps[i] > td->max ? td->temps[i] : td->max;
}
}
int main(int argc, char *argv[])
{
// Moved user input into the main() from read_temps()
// now read_temps() only needs to read and record the temp data
struct temp_data *td = temps_init();
printf("Enter .txt file name \n");
scanf("%123s", td->fname);
strcat(td->fname, ".txt");
read_temps(td);
calc_results(td);
print_results(td);
free(td);
return 0;
};
如果您正在使用Un*X,最好使用标准程序,并使用文件重定向进行输入和输出。在这种情况下,从标准输入和输出例程读取输入不需要更改。inputf=fopen(fname,“w”);
-->inputf=fopen(fname,“r”);
..temp[i]=fname;
fscanf(inputf,%d),&temp[i]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25