In: Computer Science
C Programming Language:
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.
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 floating point numbers, 10 lines of text, and 10 signed integers. There will be only one data item per line, and each line of text will contain no more than 40 characters (including '\n' and the NULL terminator).
The data contained in the binary file (both reading from and writing to) will be 10 floating point numbers (of type double), 400 characters, 10 signed integers. You may find it helpful to think of the 400 characters as 10 lines of 40 characters each. When writing the floating point values to the ASCII output file, write them to four decimal places.
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.
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 binary file and converting it to ASCII. The new ASCII file should match exactly with the original ASCII file.
Sample Text File:
6738.2342
23011.6540
-54238.3462
47231.0000
4.7204
-563.6238
0.0024
7892245.5238
-64927864.6289
262.2018
The quick brown fox jumped
over the lazy dog.
To program or not to program...
Department of Computer Science
CS262 is an awesome class!
The C Programming Language
is superior to all other languages
including Latin, Old Norse, Beothuk,
and Sumerian!
6738
23011
-54238
47231
4
-563
528032
7892245
-64927864
262
Given below is the code for the question. Your sample input file does not have 10 lines of text (it only has 10 floating and 10 int numbers). So I just made it 10 lines by moving the word 'jumped' to a newline. Please pass input file and output file names correctly to the programs. The output file generated from program1 should be used as input filename for program2.
6738.2342
23011.6540
-54238.3462
47231.0000
4.7204
-563.6238
0.0024
7892245.5238
-64927864.6289
262.2018
The quick brown fox
jumped
over the lazy dog.
To program or not to program...
Department of Computer Science
CS262 is an awesome class!
The C Programming Language
is superior to all other languages
including Latin, Old Norse, Beothuk,
and Sumerian!
6738
23011
-54238
47231
4
-563
528032
7892245
-64927864
262
AsciiToBinary.c
===========
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
float fnum;
int inum;
char str[41];
int i, size;
FILE *in, *out;
if(argc != 3){
printf("Please provide 2 arguments
- input and output filenames\n");
return 1;
}
in = fopen(argv[1], "r");
out = fopen(argv[2], "wb");
for(i = 1; i <= 10; i++){
fscanf(in, "%f", &fnum);
fwrite(&fnum, sizeof(float), 1,
out);
}
fgets(str, 40, in);
for(i = 1; i <= 10; i++){
fgets(str, 40, in);
size = strlen(str) + 1;
//write the length of string before
writing the string itself
fwrite(&size, sizeof(int), 1,
out);
fwrite(str, sizeof(char), size,
out);
}
for(i = 1; i <= 10; i++){
fscanf(in, "%d", &inum);
fwrite(&inum, sizeof(int), 1,
out);
}
fclose(in);
fclose(out);
return 0;
}
BinaryToAscii.c
=============
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
float fnum;
int inum;
char str[41];
int i, size;
FILE *in, *out;
if(argc != 3){
printf("Please provide 2 arguments
- input and output filenames\n");
return 1;
}
in = fopen(argv[1], "rb");
out = fopen(argv[2], "w");
for(i = 1; i <= 10; i++){
fread(&fnum, sizeof(float), 1,
in);
fprintf(out, "%.4f\n", fnum);
}
for(i = 1; i <=10; i++){
//first read size of string to be
read , then read the string
fread(&size, sizeof(int), 1,
in);
fread(str, sizeof(char), size,
in);
fprintf(out, "%s", str);
}
for(i = 1; i <= 10; i++){
fread(&inum, sizeof(int), 1,
in);
fprintf(out, "%d\n", inum);
}
fclose(in);
fclose(out);
return 0;
}