the array ends the second loop.

I put this function in a loop and the first run throught the program works fine. On the second time throught the loop the "fin.fail()" fails and the program ends. I think the variable "in_file[40] is not resetting, but i am not sure. Can anyone help?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void get_file (ifstream &fin, ofstream &fout)
{
              char in_file[40]= {}, out_file[40]= {};
              cout << "What is the name of the file containing data to be analyzed?\n";
              cin >> in_file;
              fin.open(in_file);
              if (fin.fail())
              {
                               cout << "Input file failed.\n";
                               exit(1);
              }
              cout << "What is the name of the file where you would like to store data?\n";
              cin >> out_file;
              fout.open(out_file, ios::app);
              if (fout.fail())
              {
                               cout << "imput file failed.\n";
                               exit(1);
              }
}
Every time you invoke the function elements of your character arrays are set to zero. So the problem somewhere else.
Any idea why it may not find the file for the second loop? Even if I use the same file that I used in the first loop.
maybe you forgot to close it before reopening.
This is the final function of the loop. Does it look okay? Any other ideas?


1
2
3
4
5
6
7

void close_files (ifstream& fin, ofstream& fout)
{
               fin.close();
               fout.close();
}               
   
It looks okay. But I think you should insert statements fim.clear(); and fout.clear();
Awsome, that worked. What does that do? Any reason to put the fout.close() since it is working? I assume that I wrote it correctly, before the close()? Sorry for all the questions. Been trying to figure that out for about 8 hours....
Thanks, gave me my life back.

1
2
3
4
5
6
7
void close_files (ifstream& fin, ofstream& fout)
{
               fin.clear();
               fin.close();
               fout.close();
}               
    

Topic archived. No new replies allowed.