Hey guys and gals,
Below is my code, my input file, and the output. I can't figure out why it is adding an extra zero at the end though. Can anyone help me with this?
CODE:
INPUT FILE:Code:#include <stdio.h> #include <stdlib.h> int main(void) { FILE *ofp; FILE *ifp; int input; int sum = 1; int output[60]; char filename[50]; //Opens the file being printed to ofp = fopen("output.dat", "w"); if(ofp == NULL) { printf("Unable to open output.dat for writing\n"); exit(1); } //Opens up the file with numbers in it ifp = fopen("pix.dat", "r"); if(ifp == NULL) { printf("Unable to open pix.dat\n"); exit(1); } int i = 0; //fscanf(ifp, "%d", &input); while ( !feof(ifp) ) { fscanf(ifp, "%d", &input); fprintf(ofp, "%d,",input ); //output[i] = input; if(sum%20==0) { fprintf(ofp, "\n"); } i++; sum++; } return 0; }
OUTPUT FILECode:2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 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------------------------------------------
I know that it is frowned upon to use feof to control a loop. Please disregard this mistake ;P Thanks!!!