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.
|
#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;
|
cout << "Writing the data to the file.\n";
|
file.close();
|
// Read the contents of the file into the array.
cout << "Now reading the data back into memory.\n";
|
// Display the contents of the array.
for (int count = 0; count < SIZE; count++)
cout << numbers[count] << " ";
cout << endl;
|
file.close();
return 0;
}
Please fill in the blanks
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;
}
Get Answers For Free
Most questions answered within 1 hours.