Как на C преобразовать конец строк в формат Windows кроссплатформенно?
#include
#include
void convert_to_crlf(const char *input_filename, const char *output_filename) {
FILE *input_file = fopen(input_filename, "r");
if (input_file == NULL) {
perror("Failed to open input file");
exit(EXIT_FAILURE);
}
FILE *output_file = fopen(output_filename, "w");
if (output_file == NULL) {
perror("Failed to open output file");
fclose(input_file);
exit(EXIT_FAILURE);
}
int ch;
while ((ch = fgetc(input_file)) != EOF) {
if (ch == '\n') {
fputc('\r', output_file);
fputc('\n', output_file);
} else if (ch == '\r') {
fputc('\r', output_file);
if ((ch = fgetc(input_file)) != EOF && ch != '\n') {
fputc(ch, output_file);
} else {
fputc('\n', output_file);
}
} else {
fputc(ch, output_file);
}
}
fclose(input_file);
fclose(output_file);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s