Hey everyone,
here is my code, I'm trying to read a .txt file into a 2D array...can anyone tell me what's wrong with my code? Thanks!!!!
Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 //Function Declarations char getNames(char namesArray[][MAX_COLS_NAMES], char filename[]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int i = 0; int j = 0; int row = 25; int col = 20; getNames (namesArray[]); return 0; } char getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp = fopen("names.txt", "r"); int i, j; int col=20; int row=25; if ( fp ) { for ( ;; ) { int c = getc(fp); if ( c == EOF ) { break; } if ( c != '\n' && c != '\r' ) { namesArray[i][j] = c; if ( ++j >= col ) { j = 0; if ( ++i >= row ) { break; } } } } fclose(fp); } for ( i = 0; i < row; i++ ) { for ( j = 0; j < col; j++ ) { putchar(namesArray[i][j]); } putchar('\n'); } }



LinkBack URL
About LinkBacks


