Information
Historical footnote: Prior to the release of Mac OS X (circa 2000), the Mac used neither of these schemes. Instead, it used a single carriage return (CR, ASCII Ox0D).
Given a text file that contains these 4 lines of text:Looking at the ASCII bytes in hexadecimal:Roses are red. Violets are blue. Some poems rhyme. But not this one.
Windows with CR-LF (0D 0A):
Unix with LF only (0A):roses-CRLF.txt: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F -------------------------------------------------------------------------- 000000 52 6F 73 65 73 20 61 72 65 20 72 65 64 2E 0D 0A Roses are red... 000010 56 69 6F 6C 65 74 73 20 61 72 65 20 62 6C 75 65 Violets are blue 000020 2E 0D 0A 53 6F 6D 65 20 70 6F 65 6D 73 20 72 68 ...Some poems rh 000030 79 6D 65 2E 0D 0A 42 75 74 20 6E 6F 74 20 74 68 yme...But not th 000040 69 73 20 6F 6E 65 2E 0D 0A is one...
The prototypes for the functions look like this:roses-LF.txt: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F -------------------------------------------------------------------------- 000000 52 6F 73 65 73 20 61 72 65 20 72 65 64 2E 0A 56 Roses are red..V 000010 69 6F 6C 65 74 73 20 61 72 65 20 62 6C 75 65 2E iolets are blue. 000020 0A 53 6F 6D 65 20 70 6F 65 6D 73 20 72 68 79 6D .Some poems rhym 000030 65 2E 0A 42 75 74 20 6E 6F 74 20 74 68 69 73 20 e..But not this 000040 6F 6E 65 2E 0A one..
Here is a header file that you should use (fileconv.h):enum FILE_ERR win2unix(const char *finput, const char *foutput); enum FILE_ERR unix2win(const char *finput, const char *foutput);
#define CR 0x0D /* Carriage Return */
#define LF 0x0A /* Line Feed */
/* Possible file errors */
enum FILE_ERR {feNONE, feINPUT, feOUTPUT};
enum FILE_ERR win2unix(const char *finput, const char *foutput);
enum FILE_ERR unix2win(const char *finput, const char *foutput);
Here is a driver file: (main.c, HTML
Text) You must specify the file names on the command line, along
with the target format:
This will convert the Windows file some_winfile.txt to a Unix file named some_unixfile.txtfileconv unix some_winfile.txt some_unixfile.txt
If you don't provide 3 arguments to the program, it will display this message:
Usage: fileconv target input_file output_file where: target is either win or unix (the resulting format) input_file is the file to convert output_file is the newly converted file Example: (Converts a Windows text file to a Unix text file) fileconv unix file-with-CRLF.txt file-with-LF.txt
The name of your implementation file should be fileconv.c and the command to compile it will look like this:
gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c fileconv.c -o fileconv
Approximate number of lines of code for each function: 15.
Notes
We are going to assume that, if you can open the file, then you can do the conversion. It is possible to get a read error during the conversion, but you don't have to handle that at this point.
Be sure you have the -n or you'll overwrite your original files! The unix2dos program works similarly. Type:dos2unix -n input.txt output.txt
To get detailed information on how to use the two programs.man dos2unix