Using c nite a main routine in which the program will reques

Using c++


nite a main routine in which the program will request the user to enter a file name, aen attempt to opca the file wih lhal name. f the file does not exist, display an emot message 3) the file does exist bat contains NO data, display an error message. function that has the lf the file does exist and contains data then your program calls a following interface: void readIntFilek ifstream &ex;, im intArray l int size, int &length;, void printFileValuesint intAmayIL int &length;) Inside method readlntFile, the code should read all the integers in the file and store them into array and return the total number of integers stored in that file in variable length. You must detig a while with a ic for end of file eon eandition: xeofo where eof( the end of afile and is a member fwnclion of fstream class. The variable sze is the declared size of the array, The size of the array for this program is 20. Remember that you are required to perform acheck no ensure you do to overnow the The method print File values will print the values that are stored inthe array. Use proper labelling Use an assetion to stop the program if there are no values in the array prim. Since this should never happen but is a \"safeguard\'\" in case it does how will you1est this? You must run tests to verify the correctness of your program.

Solution


// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <iomanip> // std::setprecision

using namespace std;

void readIntFile(ifstream &inFile, int intArray[], int size, int &length)
{
int i = 0, number;
while (true)
{
inFile >> number;
intArray[i] = number;
i++;

// protect overflow
if( i == size)
break;

// break at end of file
if(inFile.eof())
break;

}
inFile.close();

length = i;
}


void printFilevalue(int intArray[], int &length)
{
cout << \"Array: \";
for (int i = 0; i < length; ++i)
{
cout << intArray[i] << \" \";
}
cout << endl;
}

int main()
{   
int size = 20;
int intArray[size];
int length;
ifstream inFile (\"input.txt\");
if (inFile.is_open())
{
if ( inFile.peek() == std::ifstream::traits_type::eof() )
{
cout << \"FIle is empty\ \";
return 0;
}
readIntFile(inFile, intArray, size, length);
}
else cout << \"Unable to open file\";

printFilevalue(intArray, length);
  
return 0;
}

/*
input.txt
10245
10672
30403
20034
10334
29776
31123
11267
32001
10998
10003
29984
25650
32761
10099
22222

output:

Array: 10245 10672 30403 20034 10334 29776 31123 11267 32001 10998 10003 29984 25650 32761 10099 22222


*/

Using c++ nite a main routine in which the program will request the user to enter a file name, aen attempt to opca the file wih lhal name. f the file does not e
Using c++ nite a main routine in which the program will request the user to enter a file name, aen attempt to opca the file wih lhal name. f the file does not e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site