i merged files But...

closed account (1vf9z8AR)
the mreged file gives the output in one line

but i use w1<<line<<"\n".

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<<"\n";
        cout<<endl;
        cout<<"Enter more lines?(y/n)";cin>>ch;
        cout<<endl;
    }while(ch=='y');
    w1.close();
}
void merger()
{
    remove("new.txt");
    ofstream newfile;
    ifstream w1,q1;
    w1.open("file1.txt");
    q1.open("file2.txt");
    newfile.open("new.txt");
    newfile<<w1.rdbuf()<<"\n"<<q1.rdbuf();
    newfile.close();
        w1.close();
        q1.close();
}
void readmerged()
{
    ifstream r1;
    char line[80];
    r1.open("new.txt");
     while(r1>>line)
     {
         cout<<line;
     }
     r1.close();
}
int main()
{
    cout<<"WHILE WRITING FILE USE DOT TO END INPUT"<<endl;
    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();
    readmerged();
    return 0;
}
What happens if you use "\r\n" ?
closed account (1vf9z8AR)
nothing, same result
> the mreged file gives the output in one line

Your program generates the output in one line.

To get the output in multiple lines, modify lines 39 and 41:
1
2
3
4
     while( /*r1>>line*/ r1.getline( line, 80 ) ) // read line by line
     {
         cout<<line << '\n' ; // put a new line after each line
     } 


Or the much simpler:
void readmerged() { std::cout << std::ifstream("new.txt").rdbuf() ; }
Hello suyashsing234,

I think it might be your understanding of how "rdbuff()" works. On line 29 add "XXX" after the "\n" and see what you get.

Then consider what JLBorges has shown you.

Hope that helps,

Andy
closed account (1vf9z8AR)
@JLBorges
I did your thing but now every word is in different line instead of every line being in different line.
closed account (1vf9z8AR)
@Handy Andy
xxx comes written
Last edited on
closed account (1vf9z8AR)
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
#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<<"\n";
        cout<<endl;
        cout<<"Enter more lines?(y/n)";cin>>ch;
        cout<<endl;
    }while(ch=='y');
    w1.close();
}
void merger()
{
    remove("new.txt");
    char s[80];
    ofstream newfile;
    ifstream w1,q1;
    w1.open("file1.txt");
    q1.open("file2.txt");
    newfile.open("new.txt");
    while(w1>>s)
        newfile<<s;
    while(q1>>s)
        newfile<<s;
    newfile.close();
        w1.close();
        q1.close();
}
void readmerged()
{
    ifstream r1;
    char line[80];
    r1.open("new.txt");
     while(r1.getline( line, 80 ))
     {
         cout<<line<<endl;
     }
     r1.close();
}
int main()
{
    cout<<"WHILE WRITING FILE USE DOT TO END INPUT"<<endl;
    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();
    readmerged();
    return 0;
}


same problem:(
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
#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 merged_file )
{
    std::ofstream(merged_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", merged_file = "new.txt" ;

    create(in_file_1);

    create(in_file_2);

    cat( in_file_1, in_file_2, merged_file );

    std::cout << "Your files are concatenated into '" << merged_file << "'\n"
              << "the merged file looks like this:\n---------------------------\n" ;
    std::cout << std::ifstream(merged_file).rdbuf() ;
}
closed account (1vf9z8AR)
i came to know i am not allowed to use rdbuf :( :(

some one please run my code and tell why each word is in different line instead of every line being in different line
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
#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<<"\n";
        cout<<endl;
        cout<<"Enter more lines?(y/n)";cin>>ch;
        cout<<endl;
    }while(ch=='y');
    w1.close();
}
void merger()
{
    remove("new.txt");
    char s[80];
    ofstream newfile;
    ifstream w1,q1;
    w1.open("file1.txt");
    q1.open("file2.txt");
    newfile.open("new.txt");
    while(w1>>s)
        newfile<<s<<"\n";
    while(q1>>s)
        newfile<<s<<"\n";
    newfile.close();
        w1.close();
        q1.close();
}
void readmerged()
{
    ifstream r1;
    char line[80];
    r1.open("new.txt");
     while(r1.getline( line, 80 ))
         cout<<line<<"\n";
     r1.close();
}
int main()
{
    cout<<"WHILE WRITING FILE USE DOT TO END INPUT"<<endl;
    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();
    readmerged();
    return 0;
}


Hello suyashsing234,

Because when you merge the files you are reading only one word at a time with "w1 >> s" not the whole line and then writing one word with a "\n" at the end.

If you use "<string>" instead of "<string.h>" it would be easier to use std::getline(w1, line)" and then write "line" to the new file. I imagine you could do the same with "w1.getline(...)", but I am not that familiar with that version.

Hope that helps,

Andy
> some one please run my code and tell why each word is in different line
> instead of every line being in different line

It is not necessary to run your code to tell you why it is so.

1
2
3
4
    while(w1>>s) // for each word in w1
        newfile<<s<<"\n"; // write that one word followed by a new line
    while(q1>>s)  // for each word in q1
        newfile<<s<<"\n"; // write that one word followed by a new line 


Use getline() if you want to read a complete line.
closed account (1vf9z8AR)
i understand what you are saying.

This method would mess up my search function as it would not be able to read words inside lines.

Anyway i have to make separate programs for both so it doesnt matter.

Thankyou.
Last edited on
Topic archived. No new replies allowed.