Question

In: Computer Science

Write a C program that Reads a text file(any file)  and writes it to a binary file....

Write a C program that

  • Reads a text file(any file)  and writes it to a binary file.
  • Reads the binary file and converts it to a text file.

Solutions

Expert Solution

I have written the program using C PROGRAMMING LANGUAGE.

TEXT TO BINARY CONVERSION :

INPUT :

OUTPUT :

CODE :

/*Included the required libraries*/

#include <stdio.h>

#include <string.h>

#define MAX 256 // constant defined

//main method

int main() {

int num;

FILE *fp1, *fp2;

char ch, src[MAX], tgt[MAX];

/* input file name from the user */

printf("Enter your input file name: ");

scanf("%s", src);

/* output filename from the user */

printf("Enter your output file name: ");

scanf("%s", tgt);

//opened the source file in read mode

fp1 = fopen(src, "r");

if (!fp1) {

printf("Unable to open the input file!!\n");

return 0;

}

//opened the target file in read mode

fp2 = fopen(tgt, "wb");

if (!fp2) {

printf("Unable to open the output file!!\n");

return 0;

}

/* read data from input file and write

the binary form of it in output file*/

while (!feof(fp1)) {

/* reading one byte of data */

fread(&ch, sizeof(char), 1, fp1);

/* converting the character to ascii integer value */

num = ch;

/* writing 4 byte of data to the output file */

fwrite(&num, sizeof(int), 1, fp2);

}

/* close all opened files */

fclose(fp1);

fclose(fp2);

return 0;

}

BINARY TO TEXT CONVERSION :

INPUT :


OUTPUT :

   .db   0x57, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
   .db   0x43, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
   .db   0x6F, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00
   .db   0x6D, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00
   .db   0x61, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00
   .db   0x20, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
   .db   0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00
   .db   0x28, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00
   .db   0x20, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
   .db   0x61, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
   .db   0x77, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
   .db   0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
   .db   0x20, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00
   .db   0x69, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
   .db   0x79, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
   .db   0x6C, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00
   .db   0x52, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00
   .db   0x73, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
   .db   0x6E, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00
   .db   0x20, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00
   .db   0x64, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
   .db   0x6E, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
   .db   0x74, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
   .db   0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
   .db   0x20, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
   .db   0x65, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
   .db   0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00
   .db   0x2E, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0xFFFF,

CODE :

/*Included the required libraries*/

#include <stdio.h>

#include <string.h>

#define MAX 256 // constant defined

//main method

int main() {

int num;

FILE *fp1, *fp2;

char ch, src[MAX], tgt[MAX];

unsigned short val, word_count;

char buffer[20];

/* input file name from the user */

printf("Enter your input file name: ");

scanf("%s", src);

/* output filename from the user */

printf("Enter your output file name: ");

scanf("%s", tgt);

//opened the source file in read mode

fp1 = fopen(src, "rb");

if (!fp1) {

printf("Unable to open the input file!!\n");

return 0;

}

//opened the target file in read mode

fp2 = fopen(tgt, "wt");

if (!fp2) {

printf("Unable to open the output file!!\n");

return 0;

}

/* read data from input file and write

the binary form of it in output file*/

word_count = 0;

while (!feof(fp1)) {

if (word_count == 0) {

fwrite("\t.db\t",1,5,fp2);

}

val = fgetc(fp1);

if (word_count < 15) {

sprintf(buffer, "0x%02X, ", val);

word_count++;

}

else {

sprintf(buffer, "0x%02X\n", val);

word_count = 0;

}

fwrite(buffer, 1, strlen(buffer), fp2);

}

/* close all opened files */

fclose(fp1);

fclose(fp2);

return 0;

}

Thanks..


Related Solutions

2. Write a program which reads in the text file generated in part one and writes...
2. Write a program which reads in the text file generated in part one and writes out the same data as a comma-separated values (CSV) file for further processing. The file should have a header line which identifies which column contains which values and should look something like this: Time, Potentiometer, Temperature, Light, Switch0, Switch1, Switch2, Switch3 That header line should be followed by detail lines containing the measurements and should look something like this (matching the above Arduino output):...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT