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...
Write a program in C that takes a file name as the argument on the command...
Write a program in C that takes a file name as the argument on the command line, and, if the file is a symbolic link, prints out the contents of that link (i.e. the file name that the link points to). If it is not a symbolic link, the program should print an error int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR :...
C++ Part B: (String) Program Description: Write a word search program that searches an input data...
C++ Part B: (String) Program Description: Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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.
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.
Movie Structure File Write a program that will read in a CSV file with information regarding...
Movie Structure File Write a program that will read in a CSV file with information regarding movies. The file will contain a movie name, MPAA coding (G, PG, PG-13, R), number of minutes, and ratings (1-10) from the three top critics. Your program will read in the data into an array of structures (No more than 20 movies) and send the array to a function that will produce a professional report file (MovieReport.txt) which includes all of the information, an...
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