Question

Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...

Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary

                                                       data, where the data is not char type.              (Typecasting is required)

Fill in the blanks, then enter the code and run the program.

Note:   The data is int type, so typecasting is

           required in the write() and read() functions.

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

   const int SIZE = 10;

   int numbers[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   fstream file;

(1.)    Write one statement to open

        the file for output in binary mode.

The file name is:   numbers.dat

                                                  

  

   cout << "Writing the data to the file.\n";

(2.)    Write one statement to write the

           contents of the array to the file.

                                              

   file.close();

(3.)    Write one statement to open

        the file for input in binary mode.

The file name is:   numbers.dat

                                               

   // Read the contents of the file into the array.

   cout << "Now reading the data back into memory.\n";

(4.)    Write one statement to read the

        contents of the file into the array.

                                               

   // Display the contents of the array.

   for (int count = 0; count < SIZE; count++)

      cout << numbers[count] << " ";

   cout << endl;

/* OUTPUT

Writing the data to the file.

Now reading the data back into memory.

1 2 3 4 5 6 7 8 9 10

   file.close();

   return 0;

}

Please fill in the blanks

Homework Answers

Answer #1

Hey buddy,

Here you can find your answer!!!!!

#include <iostream>

#include <fstream>

using namespace std;

int main()

{
const int SIZE = 10;

int numbers[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


fstream file;


ofstream wf("numbers.dat", ios::out | ios::binary);


cout << "Writing the data to the file.\n";

for(int i = 0; i < 10; i++)
{
wf.write((char *) &numbers[SIZE], sizeof(numbers));
}
wf.close();

cout << "Now reading the data back into memory.\n";

ifstream rf("numbers.dat", ios::out | ios::binary);

for(int i = 0; i < 3; i++)
{
rf.read((char *) &numbers[i], sizeof(numbers));
}
rf.close();


for(int i=0; i < 10; i++)
{
cout << numbers[i] << endl;
}
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Lab 6    -   2 Programs   -   (Lec. 6) Lab 6    -   Program #1   -   Write one...
Lab 6    -   2 Programs   -   (Lec. 6) Lab 6    -   Program #1   -   Write one number to a text file. // This program writes one number two times to a text file. (see output below) // The program uses the setprecision and fixed manipulators to format file output. // The first output simply writes the number to file, without formatting decimal places. // The second output formats the number to 2 places to the right of the decimal point....
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
JAVA Write a program that has two functions in which the user chooses which one to...
JAVA Write a program that has two functions in which the user chooses which one to perform; 1. reads in a CSV file of integers into an array and insert it into a binary search tree class 2. deserializes an object and inserts it into a binary search tree class the tree class must be in a separate class from the CSV file read and deserialize object load
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
contents  Please use the DFT to convert the given sound file (wav) signal from time...
contents  Please use the DFT to convert the given sound file (wav) signal from time domain to frequency domain and output vibration. The text size of the size and phase size  Program requirements Requires the use of argc and argv Enter XXX.exe input.wav magnitude.txt phase.txt in cmd It can be executed. The input is the audio file (input.wav), and the output is amplitude and phase. Small text file (magnitude.txt/phase.txt) “ “rb” is used for the read mode and...
Following are the most important file management functions available in C. Use any 6 of them...
Following are the most important file management functions available in C. Use any 6 of them in your program and show the output. Function purpose fopen () Creating a file or opening an existing file fclose () Closing a file fprintf () Writing a block of data to a file fscanf () Reading a block data from a file getc () Reads a single character from a file putc () Writes a single character to a file getw () Reads...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT