Question

In: Computer Science

C++ Goals: Write a program that works with binary files. Write a program that writes and...

C++

Goals:

Write a program that works with binary files.

Write a program that writes and reads arrays to and from binary files.

Gain further experience with functions.

Array/File Functions

Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file.

write another function named fileToArray. This function should accept three arguments: the name of a file, apointer to an int array, and the size of the array. The function should open the specified file in binary mode, read its contents into the array, and then close the file.

Write a complete program that demonstrates these functions by using the arrayTofile function to write an array to a file, and then using the fileToArray function to read the data from the same file. After the data are read from the file into the array, display the array's contents on the screen.

Solutions

Expert Solution

/*
* Please note that the compiler i used to comile the code is Visual Studio Cummunity 2019.
* If you face any errors while compiling the code, please make suitable adjustments like
* #include <iostream.h>
* #include <fstream.h>
* Avoiding the use of std::
* etc
*/

#include <iostream>
#include <fstream>

int arrayToFile(char* file, int* arr, int n)
{
   std::ofstream fout(file, std::ios::binary);
   if (fout.fail())
       return 0;

   fout.write((char*)arr, n * sizeof(arr[1]));

   fout.close();
   return 1;
}

int fileToArray(char* file, int* arr, int n)
{
   std::ifstream fin(file, std::ios::binary);
   if (fin.fail())
       return 0;

   fin.read((char*)arr, n * sizeof(arr[0]));
   fin.close();
   return 1;
}

int main()
{
   int original[10] = { 8,1,44,13,11,13,22 };
   // Writing the array to the file
   arrayToFile((char*)"file.obb", original, 10);


   int duplicate[10];
   // Reading the contents already written to the file
   fileToArray((char*)"file.obb", duplicate, 10);

    // Displaying Contents to the screen

   for (int i = 0; i < 10; ++i)
   {
       std::cout << duplicate[i] << " ";
   }

   return 0;
}


Related Solutions

Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT