Reputation: 31
I'm debugging a problem with fprintf. I was hoping you guys can give me some advice or even if it is a simple problem then help me identify it. My code is as follows.
while(fgets(buffer, 500, filep) != NULL){
//printf("%d\n", i);
strcpy(result, buffer);
result = strtok(result, " ,()[]\'");
//printf("%s\n", buffer);
while(result != NULL){
//printf("%s\n", buffer);
if(stricmp(result, strng) == 0){
//printf("found!!\n");
printf("%s\n", buffer);
fprintf(Compilation, "%s", buffer);
//printf("%s", result);
}
result = strtok (NULL, " ,()[]\'");
}
result = (char*)realloc(result, 500);
}
I know this is messing and perhaps not even the best way to go at it but it is what I have. buffer and result are originially malloced with 1024 bytes. once each line is parsed, if a string is found within the line then the whole line is appended to a new file. I know the memory is messy so I'm still trying to tighten it up. My problem is that fprintf will run for many lines and once in awhile it will hit a line and crash the application. When I debug this problem in the IDE (VSexpress2012) with the printf line then I see that printf actually prints many lines after fprintf stops printing to the file. Any help will be appreciated.
Upvotes: 0
Views: 1369
Reputation: 44181
You can't pass an arbitrary pointer into realloc
. The problem is that strtok
modifies the result
pointer, then you attempt to realloc
using this pointer. realloc
needs the original pointer returned from malloc
.
Upvotes: 2