Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Binary file is 2,000,000 bytes (not including in this posting).....includes

ID: 3598796 • Letter: C

Question

C++

Binary file is 2,000,000 bytes (not including in this posting).....includes hexadecimal and ASC II, particulary needs to locate all 3's in the file

The Count3s program opens a binary file containing 32 bit integers (ints). Your program will count the number of occurrences of the value 3 in this file of numbers. Your objective is to learn about opening and accessing files and apply your knowledge of control structures. The name of the file containing data used by the program is "threesData.bin" 1. when you can verify that the file is open (hint), begin to count the int '3' until the end of file (hint) by use of a conditional statement. If you havent reached the end of the file, the file should remain open. 2. record the number of 3s by assigning it to a variable or some other method of display. 3. close the file

Explanation / Answer

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

//variable to count number of 3 in a file

int Count_3 = 0,n;

//ifstream for opening binary file for reading

ifstream in;

//open file in binary mod

in.open("threesData.bin", ios::binary);

//check if file is open

if (!in)

{

cout << "Not able to open input file" << endl;

return -1;

}

while (!in.eof())

{

in >> n;

if (n == 3)

++Count_3;

}

cout << "Total number of 3's in a binary file threesData.bin = " << Count_3 << endl;

}

---------------------------------------------------------------

//binary fil has following data

12 9 7 8 3 10 6 9
67 978 098 654 4 3
7 98 56 4 3 57 090
676 898 9098 3 900

/*output

Total number of 3's in a binary file threesData.bin = 4

*/