Can anyone give me the code to make pelles C read numbers from a file, and then write to another file the average, high, and low temperatures, in that order.
Printable View
Can anyone give me the code to make pelles C read numbers from a file, and then write to another file the average, high, and low temperatures, in that order.
I daresay yes. What have you tried?
#include <stdlib.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
#define inp_file "c:/code/temp/temps.txt"
#define out_file "c:/code/temp/averages.txt"
#define debug 1
int main(void) {
FILE *inp, *out;
double temp, average, high, low, input;
inp = fopen(inp_file,"r");
out = fopen(out_file,"w");
if (debug) {
if (inp==NULL || out == NULL) {
printf("File does not exist\n");
return(0); } }
while (fscanf(input,"%d",&temp) != EOF) {
printf("%s %lf\n",temp);
fprintf(out,"%s%lf\n",temp); }
fclose(inp);
fclose(out);
return(0);}
this is my code, but its not working and I cant quite figure out why.
Read this -> http://cboard.cprogramming.com/c-pro...ead-first.html
Then post your code again, this time using code tags to preserve some sense of indentation.
Though from editing your post, I see that it doesn't have any indentation to begin with.
1) You expect temp to hold a double, yet your fscanf() is formatted to read int.
2) printf("%s %lf\n",temp); expects two parameters. Same with the next fprintf statement.