Reading and Writing files

This program is supposed to read a file, in this case a file with 10 grades in it, and output the average. After it gets output it asks what file the user wants to save it in and then it writes it to that file.

I need help on getting my files to open and save properly and how to pass the value of the array grades[] to the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <fstream>
using namespace std;

#define MAX 10

void getGrades(int grades[], int num)
{
   for (int i = 0; i < num; i++)
   {
      cout << "Grade " << endl;
      cin >> grades[i];
   }
}

int averageGrades(int grades[], int num)
{
   int sumOfGrades = 0;
   int average;

   for (int i = 0; i < MAX; i++)
   {
      if (grades[i] == -1)
      {
         num--;
         continue;
      }
      sumOfGrades += grades[i];
   }

   if (num == 0)
   {
      return false;
   }

   else
   {
      average = sumOfGrades / num;
      return average;
   }
}

int readFile(int grades[], int num, const char fileName[])
{
   //Declare and open the output stream
   ifstream fin("fileName.txt");
   if (fin.fail())
      return false;

   //Read the data
   int data;
   fin >> data;
   if (fin.fail())
      return false;

   //Close the stream
   fin.close();

   return data;
}

bool writeFile(int grades[], int num, const char fileName[])
{
   //Declare the output stream
   ofstream fout;

   //Open the file
   fout.open("fileName.txt");
   if (fout.fail())
      return false;

   //Write the text
   fout << grades[num];
   if (fout.fail())
   {
      fout.close();
      return false;
   }

   ////Close the stream
   fout.close();
   return true;
}

int main()
{
   //get source filename
   char fileName[256];
   cout << "Source file: ";
   cin >> fileName;

   int grades[MAX];
   getGrades(grades, MAX);

   readFile(grades, 10, fileName);
   if (!readFile(grades, 10, fileName))
   {
      cout << "File " << fileName << " was unavailable for writing\n";
   }

   int average = averageGrades(grades, MAX);

   if (average == false)
   {
      cout << "Average Grade: ---%" << endl;
   }

      else
   {
      cout << "Average Grade: " << average << "%" << endl;
   }

   cout << "Destination File: ";
   cin >> fileName;

   writeFile(grades, 10, fileName);

   cout << "File written succesfully" << endl;

   return 0;
}
Woah that code looks way longer and more complex than it needs to be. I'm assuming that the grades are just numbers. All you have to do is have a for loop that executes ten times. Each time read in the next line and add the number to the total. Then divide by 10 and create an ofstream to write to the file the user enters, right? At most you should have one function and half as many lines.

Take a good read here: http://cplusplus.com/doc/tutorial/files/

Also try to use constants as opposed to defines.
Last edited on
I know its super long but my professor wants us to us separate functions for everything. We are required to have getGrades(), averageGrades(), readFile(), and writeFile() as our functions.

I'm just having a hard time understanding how the I/O Streams work. I'll keep reading the tutorial again and try and figure it out on my own, but any help would be appreciated.

I'll work on using more constants :) Thanks
Pay attention to this particular example for your readFile() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


Do something like this:
1
2
for(int i = 0; file.good(); i++)
	getline(myfile, grades[i]);
Topic archived. No new replies allowed.