No spaces between text after merging files

Hello all, I'm back again.

Theres this issue within my code, but I don't know exactly where the issue is.
The mergeFile function() takes two txt files and merges them into an empty txt file. The problem Im having is that after merging, there are no spaces between words in the "combined.txt" file. I don't know if it's something simple, or if I missed some important code that specifies to added spaces. there are no numbers in either text files, just letters and punctuation characters if that helps too.

Any help is very appreciative as always.

Thanks, liam401
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void mergeFiles(string, string, string);

int main()
{
 
  mergeFiles("Bradbury1.txt", "Bradbury2.txt", "combined.txt");
  
return 0;
}

void mergeFiles(string fileOne, string fileTwo, string combined)
{
  
  ifstream fin1; 
  ifstream fin2;
  ofstream fout;
     char ch, file1[20], file2[20], file3[30];

     fin1.open(fileOne);
     fin2.open(fileTwo);
     if(!fin1 || !fin2)
     {
          cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
          exit(EXIT_FAILURE);
     }
     fout.open(combined);
     if(!fout)
     {
          cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
          exit(EXIT_FAILURE);
     }
     while(fin1.eof()==0)
     {
          fin1>>ch;
          fout<<ch;
     }
     while(fin2.eof()==0)
     {
          fin2>>ch;
          fout<<ch;
     }
     cout<<"\n Two files have been merged into "<<combined<<" file successfully" << endl;

     fin1.close();
     fin2.close();
     fout.close();
}
Last edited on
The stream >> and << operators ignore/remove whitespace, so whitespace is never fed into fout.

If you want to preserve whitespace, you could use getline instead.
https://www.geeksforgeeks.org/getline-string-c/

Alternatively, since you appear to just want character-by-character input, you can use instream.get() and outstream.put().
http://www.cplusplus.com/reference/istream/istream/get/
http://www.cplusplus.com/reference/ostream/ostream/put/

PS:
1
2
3
4
5
     while(fin1.eof()==0)
     {
          fin1>>ch;
          fout<<ch;
     }

In general, don't loop on EOF. Loop on the successful extraction of data itself.

1
2
3
4
while (fin1 >> ch)
{
    fout << ch;
}
Last edited on
Thank you for the advice @Ganado. I'll look into the links you sent.
Also >> std::noskipws so whitespace is not skipped on stream extraction.

But simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void mergeFiles(const string& fileOne, const string& fileTwo, const string& combined)
{
	ifstream fin1(fileOne);
	ifstream fin2(fileTwo);
	ofstream fout(combined);

	if (!fin1 || !fin2 || !fout) {
		cout << "\n Invalid File Name. \n There is no such File or Directory ...";
		exit(EXIT_FAILURE);
	}

	for (char ch; fin1.get(ch); fout.put(ch));
	for (char ch; fin2.get(ch); fout.put(ch));

	cout << "\n Two files have been merged into " << combined << " file successfully" << endl;
}


Topic archived. No new replies allowed.