#include #include int main(int argc, char** pp_args) { if(argc == 1) { std::cout << "Usage: addnewline file1 file2 file3 ..." << std::endl; return 0; } else { int numErrors = 0; for(int i = 1; i < argc; ++i) { FILE* p_file = fopen(pp_args[i], "rb"); if(!p_file) { std::cerr << "Couldn't open file " << pp_args[i] << std::endl; ++numErrors; continue; } std::cout << pp_args[i] << ": "; fseek(p_file, 0, SEEK_END); long fileSize = ftell(p_file); bool addNewLine = fileSize == 0; if(fileSize) { fseek(p_file, fileSize - 1, SEEK_SET); int ch = fgetc(p_file); addNewLine = ch != '\n'; } fclose(p_file); if(addNewLine) { p_file = fopen(pp_args[i], "ab"); fputc('\n', p_file); fclose(p_file); std::cout << "added newline" << std::endl; } else { std::cout << "OK" << std::endl; } } return numErrors; } }