2016-03-26 2 views
0

So habe ich bereits eine Funktion gebaut, die 25 zufällige Temperaturen berechnet und sie ausgegeben und hatte eine maximale, minimale und durchschnittliche Funktion. Ich versuche nun, Eingabedateien und Ausgabedateien über txt einzubinden.C fragt nach Benutzer txt-Datei, Speichern in Array und Ausgabe in txt

Ich habe versucht, etwas zu recherchieren und anzuschließen, was ich könnte (auch wenn ich es kaum verstanden habe), kann jemand etwas Licht auf meinen Code geben?

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); 
}; 
+0

Wenn Sie Un arbeiten * X könnte es besser sein, ein Standardprogramm zu haben, und tun Eingabe- und Ausgabedatei Umleitung. In diesem Fall müssen die Leseeingaben von ** stdin ** und die Ausgangsroutinen nicht geändert werden. –

+1

'inputf = fopen (fname," w ");' -> 'inputf = fopen (fname," r ");' ... 'temp [i] = fname;' -> 'fscanf (eingabef, "% d", & temp [i]); ' – BLUEPIXY

Antwort

0

Es gibt ein paar Fehler im Code waren, die kritischste nicht kompilieren es nicht ist. Wenn Sie Probleme haben, sollten Sie den Anweisungen für die Erstellung eines ssce folgen. Sie werden auf diese Weise eine viel bessere Antwort bekommen. Dann erkläre klar das spezifische Problem, das du hast und was passiert oder nicht geschieht im Gegensatz zu dem, was du erwartest.

Mit Ihrem Code scheinen Sie zu Ihrem temp Array die fname Variable statt in die int Daten aus der Benutzerdatei zu lesen.

#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; 
}; 

Mit einer Datei sample.txt genannt:

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 
Verwandte Themen