mergefiles code behave badly

closed account (1vf9z8AR)
I cant understand what is happening and why it is not letting me enter more lines
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
  #include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
void write(string filename)
{
    char line[80];
    char ch;
    ofstream w1;
    w1.open(filename);
    do
    {
        cout<<"Enter line:";cin.getline(line,80);
        w1<<line;
        cout<<endl;
        cout<<"Enter more lines?(y/n)";cin>>ch;
        cout<<endl;
    }while(ch=='y');
}
void merger()
{
    char line[80];
    ofstream newfile;
    ifstream first,second;
    newfile.open("merge.txt");
    first.open("file1.txt");
    second.open("file2.txt");
    while(first>>line)
    {
        newfile<<line;
    }
   while(second>>line)
   {
       newfile<<line;
   }
   newfile.close();
   first.close();
   second.close();
   ifstream readnewfile;
   readnewfile.open("merge.txt");
   char s;
   while(readnewfile>>s)
   {
       cout<<s;
   }
   readnewfile.close();
}
int main()
{
    cout<<"Enter your first file"<<endl;
    write("file1.txt");
    cout<<"Enter your second file"<<endl;
    write("file2.txt");
    cout<<"Your files are merged"<<endl;
    merger();
}
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

void create( std::string filename )
{
    std::cout << "create file: '" << filename << '\n' ;

    std::ofstream file(filename);
    std::string line ; // favour std::string over c-style strings
    char yn ;
    do
    {
        std::cout << "Enter a line: ";
        std::getline( std::cin, line );
        file << line << '\n' ;

        std::cout << "Enter more lines?(y/n): ";
        std::cin >> yn ;
        std::cin.ignore( 1000, '\n' ) ; // throw away the new line remaining in the input buffer

    } while( std::tolower(yn) == 'y' );
}

void cat( std::string in_file_1, std::string in_file_2, std::string out_file )
{
    std::ofstream(out_file) << std::ifstream(in_file_1).rdbuf()
                            << std::ifstream(in_file_2).rdbuf() ;
}

int main()
{
    const std::string in_file_1 = "file1.txt", in_file_2 = "file2.txt", out_file = "merge.txt" ;

    create(in_file_1);

    create(in_file_2);

    cat( in_file_1, in_file_2, out_file );
    std::cout<<"Your files are concatenated into '" << out_file << "'\n" ;
}
Last edited on
At line 16, after the cin>>ch; there will be a trailing newline remaining in the input buffer. You can remove it by ignoring characters from the input until a newline is found and discarded.
1
2
        cin  >> ch;
        cin.ignore(1000, '\n');


Note: to use std::string the correct header to include is
 
#include <string> 


It can be confusing because some compilers may include other headers when say the <iostream> is included, so it may appear to work even when the required #include is missing or incorrect.


Edit: see JLBorges version above for a better example.
Last edited on
closed account (1vf9z8AR)
in cin.ignore(1000,'\n') what does 1000 do?
See: http://en.cppreference.com/w/cpp/io/basic_istream/ignore

The 1000 here is just an arbitrary large enough number.
It assumes that a user who types 'y' followed by thousand or more spaces and/or other characters before pressing enter does not deserve any sympathy.
Topic archived. No new replies allowed.