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....
Goals: Write a program that uses binary files. Write a program that stores records to a...
Goals: Write a program that uses binary files. Write a program that stores records to a binary file. C++ Requirements: Write a program that includes a structure named Part that contains the following fields: name - a character array of 20 elements that stores the name of the part. qty - an integer that stores the number of parts in stock. price - a double that stores the price of the part. Create three Part variables in main, and fill...
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...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
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...