I don't like to just dump a load of code in here and ask people to debug it for me, but I'm a bit inexperienced with C and I'm totally stumped.
The overall aim is to do a bit of cleanup on a very large log file (11G+), I'm reading in 2048 bytes at a time, then scanning individual lines, writing them to an output file. I was originally using strstr to find line endings, however I found this didn't work with the partial line at the end of the read buffer - I think this is because the "string" I'm reading from the file doesn't have a \0 at the end of it, and strstr gets confused.
So, after a bit of googling I thought I'd try memmem, which seems to be a "binary safe" drop-in replacement for strstr. This is where I'm stuck, my program is segfaulting during the call to memmem.
#include <stdio.h>
#include <string.h>
#define BUFF_LEN 2048
int main (void)
{
char file_buff[BUFF_LEN], prev_line[BUFF_LEN], curr_line[BUFF_LEN];
char *p_line_start, *p_lf;
int bytes_consumed, bytes_read;
FILE *in_fp, *out_fp;
in_fp = fopen("208.log", "r");
out_fp = fopen("expanded.log", "w+");
int sane = 0;
while (1) {
bytes_read = fread(file_buff, 1, BUFF_LEN, in_fp);
if (bytes_read == 0) {
break;
}
// Set the pointer to the beginning of the file buffer
p_line_start = file_buff;
bytes_consumed = 0;
// Chomp lines
while (bytes_consumed < bytes_read) {
printf("Read to go with bytes_read = %d, bytes_consumed = %d\n",
bytes_read, bytes_consumed);
p_lf = (char *) memmem(p_line_start, bytes_read - bytes_consumed,
"\n", 1);
if (p_lf == NULL) {
// No newline left in file_buff, store what's left in
// curr_line and break out to read more from the file.
printf("At loop exit I have chomped %ld of %d\n",
p_line_start - file_buff, bytes_read);
//break;
goto cleanup;
}
// Copy the line to our current line buffer (including the newline)
memcpy(curr_line, p_line_start, p_lf - p_line_start + 1);
printf("Chomped a line of length %ld\n", p_lf - p_line_start + 1);
fwrite(curr_line, 1, p_lf - p_line_start + 1, out_fp);
p_line_start = p_lf + 1;
bytes_consumed += p_lf - p_line_start + 1;
}
Can anyone throw me a line here?!
Tips on how to better debug this for myself are also welcome.