Created
August 5, 2016 21:18
-
-
Save DreamVB/7b98a9078f682f7a19a0874d1ca598b7 to your computer and use it in GitHub Desktop.
Convert unix line breaks to DOS line breaks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| unix2dos By DreamVB | |
| Version 1.0 | |
| Convert unix line breaks \r to dos \r\n | |
| Please feel free to to use as you like. | |
| Please let me know if you found this code usfull e. dreamvb@outlook.com | |
| */ | |
| #include <stdio.h> | |
| #include <string> | |
| int main(int argc, char* argv[]){ | |
| FILE *fin = NULL; | |
| FILE *fout = NULL; | |
| char c = '\0'; | |
| char d = '\0'; | |
| if(argc < 3){ | |
| printf("Usage: <InFile> <OutFile>\n"); | |
| return 0; | |
| } | |
| //Open inputfile. | |
| fin = fopen(argv[1],"rb"); | |
| //Try and see if inputfile opened. | |
| if(fin == NULL){ | |
| printf("IO/Error cannot open file\n"); | |
| return 0; | |
| } | |
| //Open output file | |
| fout = fopen(argv[2],"wb"); | |
| //See if output file opened. | |
| if(!fout){ | |
| printf("IO/Error cannot write file\n"); | |
| return 0; | |
| } | |
| while(!feof(fin)){ | |
| c = fgetc(fin); | |
| if(c != EOF){ | |
| if(c == '\r'){ | |
| //Check next char | |
| d = fgetc(fin); | |
| if(d != '\n'){ | |
| fputc(c,fout); | |
| fputc('\n',fout); | |
| continue; | |
| } | |
| else{ | |
| fputc('\r',fout); | |
| fputc('\n',fout); | |
| continue; | |
| } | |
| } | |
| fputc(c,fout); | |
| } | |
| } | |
| //Close files | |
| fclose(fin); | |
| fclose(fout); | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment