void read_file_by_lines(const char* path, void (*callback)(const char* line)) {
FILE* file = fopen(path, "r");
size_t length = 0;
char* line = NULL;
while (-1 != getline(&line, &length, file)) {
callback(line);
}
free(line);
fclose(file);
}
This C function should be compatible with C++. If exceptions are off, it is. If exceptions are on then it's dangerous because if an exception is thrown through callback, then the file handle and line buffer will leak.C++ exceptions are predicated on the assumption that all resources are managed using RAII. In the real world, this isn't true, especially considering that C doesn't even have RAII.
Python programmers (including me) love their exceptions, because Python is a dynamic language, and exposes system resources as reference counted objects.