I'm not sure exactly what kind of performance you're looking for, but here is an example of writing each log entry to a file and then flushing the buf2 buffer for each iteration. Thus, you can reduce the size of buf1 (defined by total_buf_size) to match only one log entry:
void *buf1 = calloc(1,total_buf_size); //With this method total_buf_size can be reduced to fit just one log entry record //build buf1 up with memcpy int index; void *buf2 = buf1; FILE * pFile; pFile = fopen ("myfile.txt","w"); for (index = 0; index < log->numentries; index++) { logentry_t *entry = (logentry_t*)&log->entries[index]; memcpy(buf2,entry->entryname,sizeof(entry->entryname)); buf2 = buf2 + (int)sizeof(entry->entryname); memcpy(buf2,&entry->entrysize,sizeof(entry->entrysize)); buf2 = buf2 + (int)sizeof(entry->entrysize); memcpy(buf2,&entry->updatesize,sizeof(entry->updatesize)); buf2 = buf2 + (int)sizeof(entry->updatesize); memcpy(buf2,&entry->numupdates,sizeof(entry->numupdates)); buf2 = buf2 + (int)sizeof(entry->numupdates); int j; for (j = 0; j < entry->numupdates; j++) { memcpy(buf2,&entry->offsets[j],sizeof(entry->offsets[j])); buf2 = buf2 + (int)sizeof(entry->offsets[j]); } int k; for (k = 0; k < entry->numupdates; k++) { memcpy(buf2,&entry->sizes[k],sizeof(entry->sizes[k])); buf2 = buf2 + (int)sizeof(entry->sizes[k]); } memcpy(buf2,entry->data,entry->updatesize); fwrite(buf2, sizeof(logentry_t), sizeof(buf2), pFile); memset(&buf2, 0, sizeof(buf2)); //clear it out buf2 = buf1; //reset the pointer } fclose(pFile); free(buf2);
source share