【问题标题】:C asking for user txt file, saving in array, and outputting in txtC请求用户txt文件,保存在数组中,并以txt形式输出
【发布时间】:2016-03-26 20:25:02
【问题描述】:

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

【问题讨论】:

  • 如果您正在使用 Un*X,最好有一个标准程序,并使用文件重定向进行输入和输出。在这种情况下,从 stdin 读取输入并且输出例程不需要更改。
  • inputf=fopen(fname, "w"); --> inputf=fopen(fname, "r"); ... temp[i] = fname; --> fscanf(inputf, "%d", &amp;temp[i]);

标签: c arrays file text


【解决方案1】:

您的代码中有一些错误,最严重的错误是它无法编译。如果您遇到问题,您需要按照有关如何制作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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    相关资源
    最近更新 更多