Grades (Read and Write) program

Hey so I have been working on this program where I am supposed to read and write a file of ten grades and average those ten grades and then write the ten grades to a file. (by the way I am not too worried about the averaging function so I am not including it). Here is my code:


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


bool readFile(int grades[], int num, char fileName[])
{
assert(num > 0);
assert(fileName && *fileName);

ifstream fin;

fin.open("grades1.txt");

if (fin.fail())
return false;
for (int i = 0; i < num - 1; i++)
fin >> grades[i];

fin.close();

return 0;
}

bool writeFile(int grades[], int num, char fileName[])
{
ofstream fout(fileName);

if(fout.fail())
return false;
for (int i = 0; i < num -1; i++)
{
fout << grades[i] << " ";
}
fout.close();
}

int main()
{
int grades[10];
char fileName[256];
cout << "Source file: ";
cin >> fileName;
if (!readFile(grades, 10, fileName))
{
cout << "File " << fileName
<< " was unavailable for opening."
<< endl;
return 1;
}

cout << "Destination file: ";
cin >> fileName;
if (!writeFile(grades, 10, fileName))
{
cout << "File " << fileName
<< " was unavailable for writing."
<< endl;
return 1;
}

Here is my output (I do not understand why it will not open the file). (also input is underlined or has u's around it).

Source file: grades1.txt
File grades1.txt was unavailable for opening.

if you could tell me why my program is not reading the file that would be great.
Is the file in the executable's working directory?
what do you mean by executable's working directory?
He means did you put the text file containing the grades in the same folder as the .exe for the program? If you're using Dev-C++, put it in your file's folder. If you're using a real compiler, it should be in your Debug folder or something similar.
Topic archived. No new replies allowed.