Question
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
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, 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. This is what I have so far.
#include
#include
int main(int argc, char *argV[])
{
int numbers;
double dNumbers;
char text[100];
// declaring file descripter
FILE *inFile;
// 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);
}
// reading each line in the file untill end of file
while(fscanf(inFile, "%s", &text) != EOF)
{
printf("%s ", text);
}
// closing the file
fclose(inFile);
}
This is what is in the txt file
6738 23011 -54238 47231 4 -563 528032 7892245 -64927864 262 6738.2342 23011.6540 -54238.3462 47231.0000 4.7204 -563.6238 0.0024 7892245.5238 -64927864.6289 262.2017 The quick brown fox jumped over the lazy dog. To program or not to program... University Department of Computer Science CS262 is an awesome class! The C Programming Language is superior to all other languages including Latin, Old Norse, and Sumerian!
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