Stubbsy Stubbsy - 3 months ago 13
C++ Question

Using and Passing input files to functions

I have an input files with chars (grades) in it and am trying to input them into an array using functions and input files.
I don't beleive I am declaring / passing the input file (iFile) correctly. Any help with my sytax and parameters would be great.

Error(s):
'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]' is private

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void readInput(char gradeArray[][3], ifstream iFile);
void spitOutpt(char gradeArray[][3], ifstream iFile);

int main()
{
ifstream iFile;
iFile.open("grades.txt");
char gradeArray[5][3];

readInput(gradeArray, iFile);
spitOutpt(gradeArray, iFile);

return 0;
}

void readInput(char gradeArray[][3], ifstream iFile)
{
for(int r = 0; r < 5; r++)
{
for(int c = 0; c < 3; c++)
{
iFile >> gradeArray[r][c];
}
}

return;
}

void spitOutpt(char gradeArray[][3], ifstream iFile)
{
cout << "All Grades" << endl;
cout << left << setw(10) << "Student";
cout << left << setw(10) << "English";
cout << left << setw(10) << "History";
cout << left << setw(10) << "Math";
cout << endl;


for(int r = 0; r < 5; r++)
{
cout << "#" << left << setw(10) << r;
for(int c = 0; c < 3; c++)
{
cout << left << setw(10) << gradeArray[r][c];
}
cout << endl;
}

return;
}

Answer

Pass std::ifstream and std::ofstream by reference.

They don't have copy constructors.

void readInput(char gradeArray[][3], ifstream& iFile);
void spitOutpt(char gradeArray[][3], ifstream& iFile);

Suggestion for cleanup

Your use of ifstream as argument to spitOutpt does not seem right. Perhaps you meant that to be an ofstream.

void spitOutpt(char gradeArray[][3], ofstream& oFile);

and then change main to:

int main()
{
    ifstream iFile;
    iFile.open("grades.txt");
    char gradeArray[5][3];

    readInput(gradeArray, iFile);


    ofstream oFile;
    oFile.open("grades-out.txt");
    spitOutpt(gradeArray, oFile);

    return 0;
}