i'm trying read text file having 7 lines array this:
include
void main( )
{ file *fp;
char *buffer[7]; //buffer array of 7 pointers char int i; if ((fp = fopen("filetext", "r")) == null) { printf("there error opening file\n"); } (i=0; i<8; i++) { if (fgets(buffer[i], sizeof(char), fp)) != null) {; printf("%s\n", buffer[i]); i++ } } fclose(fp); }
** more questions why not work? fgets, fgetc, fscanf , fread automatically go next line ? automatically increment next array element?
i've been trying 3 days now, , i'm frustrated.
the problem buffers store input not allocated.
you need prepare room store strings.
change declaration
char *buffer[7]; that allocates 7 pointers, don't point anywhere significant to
char buffer[7][1024]; where 1024 longer longest line might encounter, allocates 7k bytes store data.
this way buffer[i] address 1024 bytes may written.
reuse constant in fgets second parameter.
see documentation details : http://www.cplusplus.com/reference/cstdio/fgets/
read kernighan & ritchie understand c strings http://www.iups.org/media/meeting_minutes/c.pdf , or copy examples in doc 1 pointed to. , try reading manual of function 'fgets' answers rest of questions there in first paragraph.
Comments
Post a Comment