Write a program to give you practice at creating text and binary files.

Question

Write a program to give you practice at creating text and binary files.
You will have to use the #pragma that you used for getNum(), due to the compiler not liking fopen().
Use file I/O.
Write code to create a text file. Its name must be myTextFile.txt.
It must contain two lines of content:
This is the first line.
This is the second line.
Both lines must end with the \n character.
Write code to create a binary file.
Its name must be myBinaryFile.data. It must containthe contents of the following array:
const unsignedshortkBinaryData[]= {26946, 24942, 31090,25632, 29793,8289, 28518, 8306, 28537, 33141,39308}; 

Summary

In this question, we will create one text file and one binary file. We will be creating these files using C++ code. So we will create a text file with the name “myTextFile.txt” and a binary file with the name “myBinaryFile.bin”. The text file has a “.txt” extension and the binary file has a “.bin”  extension. 

The “myTextFile.txt” file will contain two lines of data. First-line will be “This is the first line” and the second line will be “This is the second line”. These lines will be written in a text file. In the binary file, we will insert unsigned short datatype numbers. Those numbers are given in the question. We will execute the C++ code and verify the output.

Explanation

We have declared the ofstream object with the name text. And, then we have created the file with the name  “myTextFile.txt” by the text object. It is a text file with two lines of data.

Then, we have declared the ofstream object with the name binary. Then using the binary object, we have created “myBinaryFile.bin”. It is a binary file. This binary file contains a constant unsigned short type array named BinaryData with 11 unsigned short numbers. Then, we have run a loop to write each element of the BinaryData array from the binary file created. At last, we will show the contents of the file in the output.

Code

We have written the program to practice creating text and binary files.

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    /* Create a text file. */
    ofstream text; text.open("myTextFile.txt");
    /* Write the data to the text file. */
    text<<"This is the first line."<<endl;
    text<<"This is the second line. "<<endl;
    /* Create a binary file */
    ofstream binary; binary.open("myBinaryFile.bin"); 
    /* Data that is to be inserted into the binary file */
    const unsigned short BinaryData []={26946, 24942, 31090, 25632, 29793, 8289, 28518, 8306, 28537, 33141,39308}; 
    /*Write the above data into the binary file. */
    for (int i=0;i<11;i++){
        binary<<BinaryData[i]<<endl;
    }
}

Output

Creating text and binary files.

Creating text and binary files.

 

 

Also read, Write C++ statements to accomplish each of the following

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *