Skip to content

Instantly share code, notes, and snippets.

@m039
Created June 10, 2011 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m039/1019217 to your computer and use it in GitHub Desktop.
Save m039/1019217 to your computer and use it in GitHub Desktop.
Convert file from mac(\r)/unix(\n)/dos(\r\n) formats to the unix format
/* Author: m039 <flam44 (at) gmail (dot) com> */
#include <stdio.h>
#include <stdlib.h>
static FILE *src;
static FILE *dst;
int main(int argc, char **argv) {
int c;
if (argc < 3) {
fprintf(stderr, "Usage: ./tounix <from> <to>\n");
return -1;
}
src = fopen(argv[1], "r");
if (src == NULL) {
perror(argv[1]);
return -1;
}
dst = fopen(argv[2], "w");
if (dst == NULL) {
perror(argv[2]);
return -1;
}
while ((c = fgetc(src)) != EOF) {
do {
if (c == '\r') {
fputc('\n', dst);
c = fgetc(src);
if (c == EOF) {
goto out;
}
if ((c != '\n') && (c != '\r')) {
fputc(c, dst);
}
} else {
fputc(c, dst);
}
} while (c == '\r');
}
out:
fclose(dst);
fclose(src);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment