c - Read .CSV file and store it in another file -
have made program reads .csv file , stores highest number in file. problem program can't read comma separated numbers 1,5,6,7,1,2. here loop need change
int i; int max = 0; int min = 0; while (!feof(fp)) { fscanf( fp, "%d", &i); if (i < min) min = i; if (i > max) max = i; }
and print out:
fprintf(q,"%d",max); printf("maximum value %d \n", max); fclose(q); fclose(fp);
#include <stdio.h> #include <limits.h> int main(void){ file *fp = fopen("input.csv", "r"); file *q = fopen("max.txt" , "w"); int i; int max = int_min; int min = int_max; while(1){ int state = fscanf(fp, "%d", &i); if(state == 1){ if (i < min) min = i; if (i > max) max = i; } else if(state == eof){ break; } else { char ch; fscanf(fp, " %c", &ch); if(ch != ','){ fprintf(stderr, "\nformat error\n"); break; } } } fprintf(q, "%d", max); printf("maximum value %d\n", max); fclose(q); fclose(fp); return 0; }
Comments
Post a Comment