c - Get line of FILE * -
I'm trying to learn C (which is hard ...) so I wanted to write a regular one File Line I created this function:
int c; Intel Buffalo = 100; If ((* buffer = molok (size (four) * bufflanes) == faucet () (DXLogWarn ("not enough memory"); return;} char * dst = * buffer; char * end = * buffer + buflen - 1; while ((c = getc (fp))! = EOF) {if (c == '\ n') {brake;} if (dst and lt; end) {* dst ++ = c;}} * Dst = '\ 0'; Which works! But now I thought that instead of cutting the last part of the string I can reload buffer and '\ n' So I changed it to:
zero fget_line (FILE * fp, char ** buffer) {int c; int bufflan = 10; if ((* buffer = molok (size (four) * bufflanes) == faucet () (DXLogWarn ("not enough memory"); return;} char * dst = * buffer; char * end = * buffer + buflen - 1; while ((c = getc (fp))! = EOF) {if (c == '\ n') {brake;} if (DST & lt; end) {* dst ++ = c;} Other {buflen * = 2; * buffer = reel (* buffer, buffron * size (four)); if (* buffer == faucet) {DXLogError ("Err"); return;}}}} * dst = '\ 0 ';} I get an error: malloc: * Error for object 0x10a8001b8: wrong checksum for free object - the object may be free Adad was revised. * To set a breakpoint in debug to malloc_error_break which I did not find. What am i doing
Initially:
four * dst = * buffer; Char * end = * buffer + buflen - 1; This set ends up for the right position inside DST and buffer. You can then reload buffer:
* buffer = reel (* buffer, buffron * size (four)); This causes the buffer to get a new value which probably varies before it, so if you click dst and for new posts in buffer must be updated Converting is easy to change, but because you keep changing dst , the new value is difficult to calculate . Therefore, instead of changing pointers, it can be easy to store offsets in just one integer something like this: int pos = 0; While ((c = getc (fp))! = EOF) {if (c == '\ n') {break; } If (pause = gt = buflen-1) {buflen * = 2; * Buffer = reel (* buffer, buffron * size (four)); If (* buffer == tap) {DXLogError ("Err"); Return; }} (* Buffer) [pos] = c; Status ++; } (* Buffer) [POS] = '\ 0'; I have also changed the above statement, as you did not store that character which caused the re -ocation.
Comments
Post a Comment