Lab 10

ASCII and Binary File I/O

Due November 5, 2014 at 11:59 PM


For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format.

Background Preparation: Review file I/O for ascii and binary formats and command line arguments

Specifications:
Your programs will use the following structure to hold the data that is read and to be written:

typedef struct _FileData
{
    int a;
    double b;
    char dataStr[56];
} FileData;


Both programs will obtain the filenames to be read and written from command line parameters:
bash$ AsciiToBinary  ascii_in  binary_out

bash$ BinaryToAscii binary_in ascii_out

The data format in the ASCII format files (both reading and writing) will be one data item per line:
47
34.278
This is a line of text
A sample ASCII  format file is provided. There will be a set of three lines for each FileData structure's data. There will be no blank lines between each set of three lines. There is no limit to the number of three line sets a file may contain. You can assume that each text string will be 55 characters or less. Note that although there will always be an integer and a floating point value, it is possible that the data string will be empty (i.e. a blank line).

The size of each record for the binary file will be the same as the size of the FileData structure.

The specific method that you use to read and write the data is up to you. You may wish to read all the data from the input file before writing to the output file, or you may wish to write each record as it is read. When writing floating point data to the ASCII file, use "%.3f" in your format string to print the precision of the floating point value to three decimal places.

Be sure to close both files (input and output) before exiting the program.

Testing:
Besides testing your code with your usual methods, you should also test your programs by reading an ASCII file, converting it to binary, then reading the converted binary file and converting it back to ASCII. The new ASCII file should match exactly with the original ASCII file.

Submission:
Create a Makefile that will compile both executables. Place this Makefile, all source files and the provided ASCII file in a single directory and create a tar file for submiission. Submit this tarfile on Blackboard as Lab 10.