I'm trying to load a simple file with text lines on it and store on a proper structure. The code i'm working on is:
A TEXTBUFFER is a struct defined as:Code:BOOL loadtextfile(char* filename, TEXTBUFFER* t) { FILE* fp; char line[MAX_LINE]; int i = 0; fp = fopen(filename, "r"); if (!fp) return FALSE; fgets(line, MAX_LINE, fp); while (!feof(fp)) { t->linebuffer[i] = (char*)xmalloc(MAX_LINE); line[strlen(line)-1] = '\0'; /* quit CR */ strncpy(t->linebuffer[i], line, strlen(line) ); i++; fgets(line, MAX_LINE, fp); } t->numTextLines = i; fclose(fp); return TRUE; }
I don't know how reading lines (from a proper formatted lines) trash other memory areas...Code:typedef struct { int numTextLines; /* numero de lineas de texto fuente*/ char* linebuffer[]; } TEXTBUFFER;![]()




Reply With Quote