gpt4 book ai didi

C 请求用户txt文件,保存在数组中,并以txt输出

转载 作者:行者123 更新时间:2023-11-30 17:03:13 25 4
gpt4 key购买 nike

所以我已经构建了一个函数,可以计算 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 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);
};

最佳答案

您的代码中存在一些错误,最关键的一个是它无法编译。如果您遇到问题,您需要按照有关如何制作 ssce 的说明进行操作。 。这样你会得到更好的回应。然后清楚地解释您遇到的具体问题以及正在发生或未发生的情况,而不是您期望的情况。

在您的代码中,您似乎将 fname 变量分配给 temp 数组,而不是从用户文件中读取 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;
};

使用名为 sample.txt 的文件:

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

关于C 请求用户txt文件,保存在数组中,并以txt输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240483/

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