Rewrite a FEOF while loop to a for loop in c

77 Views Asked by At

I need some help to rewrite the feof while loop to a for loop in c.

I have two variables n and m.

Variable n is the size of the array / 1023 in rows and m is columns that are 1023 in size.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
int n, m;
int *A;
FILE* inputFile;
inputFile = fopen("Input.dat", "r");
        
if(inputFile == NULL){
    printf("An error occured while reading File\n");
    exit(0);
}

else{
    fscanf(inputFile,"%d", &n);
    fscanf(inputFile,"%d", &m);
}

A = (int*)malloc((n*m) * sizeof(int));

if(A == NULL) {
    printf("An error occured while allocating");
    exit(0);
}

int i = 0;
//How can I rewrite this while loop to a for loop in c programming
while(!feof(inputFile)) {
    fscanf(inputFile, "%d", &A[i]);
    i++;
}   

for(int i=0; i<n; i++){
    printf("\n");
    for(int j=0; j<m; j++){
        printf("%d", A[i * m + j]);
    }
}

return 0;
}
0

There are 0 best solutions below