Read a file line by line: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: mmap method)
(→‎{{header|C}}: syntax hilight)
Line 273: Line 273:


Implementation using mmap syscall. Works on Linux 2.6.*, should work on *BSDs (they invented mmap, after all). Line reading routine takes a callback function, each line is passed into callback as begin and end pointer. Let OS handle your memory pages, we don't need no stinking mallocs.
Implementation using mmap syscall. Works on Linux 2.6.*, should work on *BSDs (they invented mmap, after all). Line reading routine takes a callback function, each line is passed into callback as begin and end pointer. Let OS handle your memory pages, we don't need no stinking mallocs.
<lang>#include <stdio.h>
<lang C>#include <stdio.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
Line 351: Line 351:
int main()
int main()
{
{
return read_lines("result.ps", print_line) ? 0 : 1;
return read_lines("some_text_file", print_line) ? 0 : 1;
}
}
</lang>
</lang>