Question
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
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, including the fread(), fwrite() and fprintf() functions.
Review your notes on Command Line Parameters and/or find information online at a site
Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example:
bash$ AsciiToBinary ascii_in binary_out bash$ BinaryToAscii binary_in ascii_out
The data contained in the ASCII file (both reading from and writing to) will be 10 signed integers, 10 floating point numbers, and 10 lines of text. There will be only one data item per line, and each line of text will contain up to 40 characters (including ' ' and the NULL terminator). The data contained in the binary file (both reading from and writing to) will be 10 signed integers, 10 floating point numbers (of type double), and 400 characters. You may find it helpful to think of the 400 characters as 10 sets of 40 characters each.
Sample ASCII and binary format files are provided. The data in the two files correspond to each other. There will be no blank lines found in either file. 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 data element as it is read. Be sure to close both files (input and output) before exiting the program. Compiling: Your programs should both compile using a single Makefile (i.e. the make command will compile both programs). Therefore, your Makefile will have at least four targets - all (which will be the uppermost target in the file), AsciiToBinary, BinaryToAscii, and clean (the last target in the file). Your executables do not need to conform to the standard naming conventions used for CS262. However, all directories and source files do need to adhere to these standards.
I am confused on how to read in the data in ASCII and convert it to binary. So far I was able to read in the values for the ints and float, but when reading in the lines of text, it reads it word by word instead of line by line. Also I am not sure how are we supposed to convert it to binary and back to ASCII. This is what I have so far:
AsciiToBinary.c
#include
#include
int main(int argc, char *argV[])
{
int numbers;
float fNumbers;
char text[100];
// declaring file descripter
FILE *inFile;
FILE *outFile = fopen("Outfile.dat", "wb");
// opening a file for reading passed by the argument
inFile = fopen(argV[1], "r");
// checking to make sure the file is opended properly
if(inFile == NULL)
{
printf("ERROR: Could not find file %s ", argV);
exit(1);
}
int itemsWritten = 0;
// reading each line in the file untill end of file
while(fscanf(inFile, "%s", &text) != EOF)
{
printf("%s ", text);
itemsWritten = fwrite(&text, sizeof(text), 1, outFile);
}
if(itemsWritten != 64)
{
printf("ERROR");
exit(1);
}
// closing the file
fclose(inFile);
fclose(outFile);
}
BinaryToAscii.c
#include
#include
int main(int argc, char *argV[])
{
char text[100];
// declaring file descripter
FILE *inFile;
FILE *outFile = fopen(argV[1], "rb");
// opening a file for reading passed by the argument
inFile = fopen("inFile.txt", "w");
// checking to make sure the file is opended properly
if(outFile == NULL)
{
printf("ERROR: Could not find file %s ", argV[1]);
exit(1);
}
// need a while loop to loop through the binary file until the EOF
//
int itemsRead = fread(&text, sizeof(text), 1, outFile);
if(itemsRead != 64)
{
printf("ERROR ");
exit(1);
}
fprintf(inFile, "%s ", text);
// closing the file
fclose(inFile);
fclose(outFile);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started