c - How to make a working read from file Function? -
int main() { char a[100][100]; int = 0, k = 0; file* file = fopen( "a:/input.txt", "r" ); if( file == null ){ perror( "error opening file !\n" ); } else { while( ! feof( file )){ fscanf( file, "%d;%s\n", &i, a[i] ); } } for( ; k < i; ++k ){ printf( "%d -> %s\n", k, a[k] ); } return 0; }
i have working piece of code , tried implement function this:
int reader(char b[20], char *a[100][100], int *i){ int j = 0; file* file = fopen(b, "r" ); if( file == null ){ perror( "error opening file !\n" ); } else { while( ! feof( file )){ fscanf( file, "%d;%s\n", j, a[j] ); } } *i = j; return 0;}
and make call main:
char a[100][100]; int = 0; reader("type.txt", &a, &i);
but when use function, code crash @ fscanf call. ideea how can make work or how can create function reads line line file , creates array in form: array[0] = firstline, array[1] = secondline, etc.
you did correctly in main()
function variable i
, when moved function forgot symbol. need pass address of j
fscanf()
:
fscanf( file, "%d;%s\n", &j, a[j] );
Comments
Post a Comment