Closing a file.

My project is to open two files, read one fil into the othe other file, then close and reopen the file that was written to. I seem to be having problems closing the file that was written to. Any suggestions or help would be useful.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>   //cin, cout
#include <string> //setw, setprecision, fixed
#include <fstream> // File stream thingy
#include <cstring>
using namespace std;  

 void openfile(fstream &outputfile);
 void inputfile(fstream &intofile, fstream &outputfile); 
 void readFile ( fstream &intofile, fstream &outputfile );
 void openfileapp(fstream &outputfile);
 void writetofile ( fstream &intofile, fstream &outputfile);

 
int main ()
{
  fstream outputfile;
  fstream intofile;
  
  openfile(outputfile);
  inputfile(intofile, outputfile);
  
  openfileapp(outputfile);
  writetofile (intofile, outputfile);  
   
system ("pause"); 
return 0; 
}

  void openfile(fstream &outputfile)
   {
    outputfile.open ("lab3input.txt"); //open in file
     if (!outputfile)
      {
      cout << "File did not open properly!1"<< endl;
      system ("pause"); 
      exit(EXIT_FAILURE);
      } 
   }
 //Purpose: to open a file and check to see if file opens 
  void inputfile(fstream &intofile, fstream &outputfile)
   {
    intofile.open ("lab3output.txt", ios::out); //open in file
    if (!intofile)
    {
    cout << "File did not open properly!2"<< endl;
    system ("pause"); 
    exit(EXIT_FAILURE);
    }  
    readFile (intofile, outputfile);    
    intofile.close ();
   }
void readFile ( fstream &intofile, fstream &outputfile )
 {
     string lname, fname, address, city, zip,
          state; 
     while (getline(outputfile, lname, '^'))
     {   
            getline (outputfile,fname, '^');
            getline (outputfile,address, '^');
            getline (outputfile,city, '^');
           // getline (outputfile,state, '^');
            getline (outputfile,zip, '^');
        // Resizing the lines 
        
            lname.resize(30,' ');
            fname.resize(30,' ');
            address.resize(20,' ');
            city.resize(30,' ');
            
        // outs the information to the output file.
   intofile << lname << fname << address<< city << state << zip<<'\n'<<endl; 
     }
      outputfile.close(); 
      if (!outputfile)
      {
      cout << " Here1"<< endl;
      }
      intofile.close();
      if (!intofile)
      { 
      cout << " Here2"<< endl;
      }
 }
 // opening the outputfile in append mode.
 void openfileapp(fstream &intofile)
   {
    intofile.open ("lab3output.txt", fstream::app); //open in file
    if (!intofile)
    {
    cout << "File did not open properly!3"<< endl;
    system ("pause"); 
    exit(EXIT_FAILURE);
    }  
    
   }
// allows the user to input data, and add onto what is already in the file.
void writetofile ( fstream &intofile, fstream &outputfile )
 {
     string lname, fname, address, city, zip,
          state; 
     cout << " Please enter your first name:"<< endl;
      cin>> fname;
      while (fname.length() < 1 || fname.length() >20)
        {
          cout << " Please reenter your first name:" << endl;
          cin >> fname;
          } 
      cout << " Please enter your last name:"<< endl;
      cin>> lname;
      while (lname.length() < 1 || lname.length() >30)
        {
          cout << " Please reenter your last name:" << endl;
          cin >> lname;
          } 
       cout << " Please enter your address:"<< endl;
      cin>> address;
      
       cout << " Please enter your City:"<< endl;
      cin>> city;
      while (city.length() < 1 || city.length() >30)
        {
          cout << " Please reenter your city:" << endl;
          cin >> city;
          } 
       cout << " Please enter your first state:"<< endl;
      cin>> state;
            while (state.length() < 1 || state.length() >2)
        {
          cout << " Please enter your state, musy be state initials:" << endl;
          cin >> state;
          } 
       cout << " Please enter your zip code:"<< endl;
      cin>> zip;
         while (zip.length() != 5)
        {
          cout << " Please reenter your zip code:" << endl;
          cin >> zip;
          } 
          
           lname.resize(30,' ');
           fname.resize(30,' ');
           address.resize(20,' ');
           city.resize(30,' ');
        // outs the information to the output file.
   intofile << lname << fname << address<< city << state << zip<<'\n'<<endl;
     }
What's the problem exactly?
Can you please describe it?
Topic archived. No new replies allowed.