|
Your own explanation has defined the logic of the program. It's not difficult to implement, a scripting language like perl can read line by line and it's easy to handle your requirements.
If it is C, the simple way is to read the file into memory at one time, and you can handle it with a few pointers. The general code is as follows (note that the program is not debugged, there may be errors):
char * buffer;
int filelen;
char * buffer_end = buffer + filelen;
buffer = (char *) malloc (filelen);
/ * read file data to buffer * /
char * line_start;
char * line_end;
char line_buffer [1024]; / * maximum line length is 1024 * /
line_end = line_start;
while (line_end <buffer_end) {
int line_offset = 0;
line_start = line_end;
while (line_end! = '\n') line_end ++;
if (line_end> = buffer_end) break;
if (the last character before line_end is '\') {
strncpy (line_buffer + line_offset, line_start, line_end-line_start);
line_offset + = line_end-line_start; // determine here whether the line_buffer size is exceeded
} else {
Process line_buffer and empty
strncpy (line_buffer + line_offset, line_start, line_end-line_start);
}
} |
|