Hi!
I have created a text file containing 20 rows of 20 ints. I am attempting to read the contents of the file into a 20x20 2D array of int. But unfortunately its not working and after many hours I am come to ask for expert help.
Here is my code thus far:
There definetley seems to be something wrong with the fread line. This is the first time I've ever used it. When I run the program i get ...Code:#include <stdio.h> #define MAX 20 #define FTWENTY "twenty.txt" main() { int my_array[MAX][MAX] = {0}; int i, j; FILE *fp; fp = fopen(FTWENTY, "r"); if (fp == NULL) { perror("Couldn't open the file" FTWENTY); exit(1); } if (fread(my_array, sizeof(int), (MAX*MAX), fp) < (MAX*MAX)) { perror("Couldn't read the file" FTWENTY); fclose(fp); exit(1); } for (i=0; i < MAX; ++i) { for (j=0; j < MAX; ++j) printf("%2d", my_array[i][j]); putchar('\n'); } fclose(fp); return 0; }
Couldn't read the filetwenty.txt: No error
Can anyone help please?
Thanks!



LinkBack URL
About LinkBacks



