c - How can I print as strings the content of a .txt file? -
like said in title don't know how print content of .txt file in c. here's incomplete function did:
void print_from_file(items_t *ptr,char filemane[25]){ char *string_temp; file *fptr; fptr=fopen(filemane, "r"); if(fptr){ while(!feof(fptr)){ string_temp=malloc(sizeof(char*)); fscanf(fptr,"\n %[a-z | a-z | 0-9/,.€#*]",string_temp); printf("%s\n",string_temp); string_temp=null; } } fclose(fptr);
}
i'm pretty sure there's errors in fscanf because doesn't exit loop.
can please correct this?
you're using malloc
wrong. passing sizeof(char*)
malloc
means giving string amount of memory take hold pointer character(array). currently, writing memory have not allocated, have undefined behavior. highly advisable perform checks on file lenght , otherwise make sure not write more string allocated it.
instead, this:
string_temp=malloc(100*sizeof(char)); // enough space 99 characters (99 chars + '\0' terminator)
Comments
Post a Comment