File Handling

Something is wrong with my program logic. I am trying to merge to files into one. The program starts, but nothing happens, it doesn't close, and the mergefile doesn't update. v.v

The first file is FemaleClients.rtf and consists of these characters:
21 Barbara_Jones
82 Sue_Todd
275 Mary_Donald
276 Cathy_Conner
300 Debbie_Baker
400 Kim_Tanner

The second file, MaleClients.rtf, consists of:
56 Bob_Johnson
100 Dan_Smith
200 Mike_Tay

They are supposed to merge into MergedClients.rtf. If you can find the error in the logic without creating those folders, awesome. If not, I understand it would take awhile to create these folders and go through trial and error. Anywho, heres my code. Don't laugh.
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
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//Declarations
         ifstream inFile1;
         ifstream inFile2;
         ofstream outFile1;
         int mClientNumber; 
         int fClientNumber;
         string mClientName; 
         string fClientName;
         bool atLeastOneFileNotAtEnd = true;
         bool inFile1Written = false;
         bool inFile2Written = false;
         
int main()
{
    cout<<"File merge processing starting"<<endl;
    inFile1.open("MaleClients.txt");
    inFile2.open("FemaleClients.txt");
    outFile1.open("MergedClients.txt");
    inFile1>>mClientNumber, mClientName;
    inFile2>>fClientNumber, fClientName;
         while(atLeastOneFileNotAtEnd==true)
         {
              if((inFile1.eof()))
              {
                   if(inFile2Written==false)
                   {
                       outFile1<<fClientNumber<<endl;
                       outFile1<<fClientName<<endl;
                       inFile2Written=true;
                   }
                  
              else if((inFile2.eof()))
              {
                   if(inFile1Written==false)
                   {
                       outFile1<<mClientNumber<<endl;
                       outFile1<<mClientName<<endl;                         
                       inFile1Written=true;
                   }
                       
              }
              else if(mClientNumber<fClientNumber)
              {
                  outFile1<<mClientNumber, mClientName;
                  inFile1Written=true;
              }
              else
              {
                  outFile1<<fClientNumber<<endl;
                  outFile1<<fClientName<<endl;
                  inFile2Written=true;
              }
         }     
         if((inFile1.eof())&&(inFile1Written==true)) 
              { 
                  inFile1>>mClientNumber, mClientName;
                  inFile1Written=false;
              }
         if(!(inFile2.eof())&&(inFile2Written==true))
              {
                  inFile2>>fClientNumber, fClientName;
                  inFile2Written=false;
              }
         if((inFile1.eof())&&(inFile2.eof()))
              {
                  atLeastOneFileNotAtEnd=false;
              } 
         }                                                             
    inFile1.close();
    inFile2.close();
    outFile1.close();

   system("PAUSE");
   return 0; 
}                


bump
just include closing curly brace } in the line 36

Because you havent close the first if statement
Last edited on
Thank you for noticing. Fixed it, and the program still doesn't close. Also, now when I open the .txt file, it says "boolean inWritten2" and then displays a zillion "0's". When I open the rtf file, still no merge :(
Line 49,61,66
take out the comma ',' and replace with appropriate '<<" or '>>'
You cannot use comma

And also there is a major error in your code. look at the while loop braces'{}'
The closing braces at line 58. The while loop will finish there. So take out that closing brace
Last edited on
closed account (DSLq5Di1)
The logic is a little mind boggling, is there any special requirements for the merged file? if not.. keep it simple,

1
2
3
4
5
6
// you mention rtf, though you are opening txt files.. is this correct?
ifstream male_clients("MaleClients.txt");
ifstream female_clients("FemaleClients.txt");

ofstream merged_clients("MergedClients.txt")
merged_clients << male_clients.rdbuf() << female_clients.rdbuf();
Well, this logic is given to my class in an algorithm and we are supposed to transfer it to c++ statements. I'm pretty new to programming, so I probably couldn't write better logic anyways...

Here is my new code, program still doesn't work. I'm curious to what the statement "merged_clients << male_clients.rdbuf() << female_clients.rdbuf();"
does, couldn't find a straight forward explanation from google.
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
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//Declarations
         ifstream inFile1("MaleClients.txt");
         ifstream inFile2("FemaleClients.txt");
         ofstream outFile1("MergedClients.txt");
         int mClientNumber; 
         int fClientNumber;
         string mClientName; 
         string fClientName;
         bool atLeastOneFileNotAtEnd = true;
         bool inFile1Written = false;
         bool inFile2Written = false;
       
         
int main()
{
    cout<<"File merge processing starting"<<endl;
    inFile1.open("MaleClients.txt");
    inFile2.open("FemaleClients.txt");
    outFile1.open("MergedClients.txt");
    inFile1>>mClientNumber>>mClientName;
    inFile2>>fClientNumber>>fClientName;
    outFile1<<inFile1.rdbuf()<<inFile2.rdbuf();
         while(atLeastOneFileNotAtEnd==true)
         {
              if((inFile1.eof()))
              {
                   if(inFile2Written==false)
                   {
                       outFile1<<fClientNumber<<fClientName<<endl;
                       inFile2Written=true;
                   }
              }    
              else if((inFile2.eof()))
              {
                   if(inFile1Written==false)
                   {
                       outFile1<<mClientNumber<<mClientName<<endl;
                       inFile1Written=true;
                   }
                       
              }
              else if(mClientNumber<fClientNumber)
              {
                  outFile1<<mClientNumber<<mClientName;
                  inFile1Written=true;
              }
              else
              {
                  outFile1<<fClientNumber<<fClientName<<endl;
                  inFile2Written=true;
              }
                   
              if((inFile1.eof())&&(inFile1Written==true)) 
              { 
                  inFile1>>mClientNumber;
                  inFile1>>mClientName;
                  inFile1Written=false;
              }
              if(!(inFile2.eof())&&(inFile2Written==true))
              {
                  inFile2>>fClientNumber;
                  inFile2>>fClientName;
                  inFile2Written=false;
              }
              if((inFile1.eof())&&(inFile2.eof()))
              {
                  atLeastOneFileNotAtEnd=false;
              } 
         }                                                             
    inFile1.close();
    inFile2.close();
    outFile1.close();
    cout<<"Merging Complete!"<<endl;
    
system("PAUSE");
return 0;
    

    
}                   
You can be a pro in c++ by taking some advise from a pro person.For this you can read some e-books.Here you can download few e-books.

<a href="http://www.get-tuto.blogspot.com/2012/09/c-c-programming-language-isbasically.html">c++</a>
closed account (DSLq5Di1)
ProductFailure wrote:
I'm curious to what the statement "merged_clients << male_clients.rdbuf() << female_clients.rdbuf();"


ios::rdbuf() returns a pointer to the stream buffer, and ostream::operator<< has an overload to output stream buffers. Internally, you could picture it as while loop reading and writing each character to the output stream.

ProductFailure wrote:
Well, this logic is given to my class in an algorithm and we are supposed to transfer it to c++ statements.
In that case, disregard the code from previous post.

Lines 34, 42, 49, 54, output a space between the number and name. Line 49 missing an endl. Line 58 missing !. !(inFile2.eof())
Sloppy, you are the man!
And eather, link is bookmarked. Thank you as well.
Topic archived. No new replies allowed.