Read and write file (combine lines in one)

I am given a text file named tr4_1.txt, the file content is below:
1110000000001111
1110100000000000
1000001111111111
0111011101010101

I would like to ask how could I edit my code below so that the output file can combine all lines into one?
1110000000001111111010000000000010000011111111110111011101010101

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
 #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

int main(void)
{
    char ip_file[32]= "tr4_1.txt";
    char opfile[32]="prefix.txt";
    string line;
    ofstream ofs; ofs.open(opfile);

    	ifstream ifs;
    	ifs.open(ip_file);

    	while(ifs.good()){
    		getline(ifs,line);

    		ofs<<line<<endl;


    	}



}
The short answer is to remove the endl at line 20.
But you should also take the opportunity to fix the while loop.

Instead of
1
2
3
    while (ifs.good()) {
        getline(ifs,line)
        // etc. 

do this:
1
2
    while (getline(ifs,line)) {       
        // etc. 


(the first version doesn't check the file status after getline, which is a logic error).
Then I would like to ask, say i have 10 such files, how could I read all the files automatically, do the similar things on each file then combine them into one file? Thanks !! Is that I should use a for loop?
You could use a for loop. Put all of the input filenames into an array of strings perhaps.
OKAY! I will try it ! Thanks lots!!
Topic archived. No new replies allowed.