Question

In: Computer Science

C Programming Language: For this lab, you are going to create two programs. The first program...

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

Solutions

Expert Solution

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;
}


Related Solutions

Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program,...
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program called randomwrite.py that writes a series of random integers to a file....
C programming language The program first creates a child process CP. So, there are two processes:...
C programming language The program first creates a child process CP. So, there are two processes: The parent process does the following: a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary; b. wait for the termination of the child process CP, then terminate; The child process does the following: a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary; b. terminates;
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
IN SWIFT LANGUAGE In this lab we are going to create a game that uses all...
IN SWIFT LANGUAGE In this lab we are going to create a game that uses all the different types of control flow operators that exist in SWIFT LANGUAGE. The game will be a choose your own adventure game. The user will have to answer a series of questions and this will change the path of a story. This must be done by using if/else statements that are nested within other if/else statements. Your story must have four (4) different possible...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT