繁体   English   中英

C询问用户txt文件,保存在数组中,然后以txt输出

[英]C asking for user txt file, saving in array, and outputting in txt

因此,我已经构建了一个函数,该函数可以计算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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM