io stream

I have these two functions that are supposed to read and save a file and then load of the save file. I'm not sure which one is messing up or if they both are or if the problem is in one of my other functions. If someone could look it over and let me know if you see anything wrong or that needs changing it would be very much appreciated.

I think it saves the file correctly but when I try and load it it outputs a whole bunch of extra lines and it won't even output the right data.

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
bool readFile(char filename[], char name[][256], int &records, int points[], int grade[])
{
   ifstream fin(filename);
   if(fin.fail())
      return false;

   fin >> records;

   int i = 0;
   while(i < records)
   {
      fin >> name[i] >> points[i] >> grade[i];
      i++;
   }
   fin.close();
   return true;
}

bool writeFile(char filename[], char name[][256], int records, int points[], int grade[])
{
   ofstream fout;
   fout.open(filename);
   if (fout.fail())
      return false;

   while (records >= 0)
   {
      fout << name[records] << " " << points[records] << " " << grade[records];
      records--;
   }
   if (fout.fail())
   {
      fout.close();
      return false;
   }
   fout.close();
   return true;
}
Both functions look ok. (except that line 26 loop will loop record+1 times even though you want it to loop record times).
Explain what exactly is your input and output. Also, if you don't know whether writing or reading fails, why not check the generated file?
Topic archived. No new replies allowed.