Double-spacing file output in C++ Program?

The user enters in an input file, which is supposed to be filtered by various subclasses which I have implemented but have nothing to do with this question. The filter I can't figure out how to do is making the file double spaced. The program outputs, character by character, the transformed data to an output file (which is blank before the program runs).

I have this function, which is part of an abstract class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void FileFilter::doFilter(ifstream &in, ofstream &out)
{
	char ch;
	char transCh;

	in.get(ch);

	while (!in.fail())
	{
		transCh = transform(ch);
		out.put(transCh);
		in.get(ch);
	}
};

and also this function, which I need to modify to be able to double-space (it currently just copies everything from the input file)
1
2
3
4
5
6
7
8
9
class DoubleSpace : public FileFilter
{
public:

	char transform(char ch)
	{
	     return ch;
	}
};

Based on that logic, how can I get it to return a double space? The transCh variable can only hold one char, so I'm stumped on how to do it short of redefining the doFilter function in the DoubleSpace subclass. Thank you.

Following this is the entire program, which is currently modified to only run the file name input, the double-space class, and the filter function. I commented the rest out to avoid having to type in all the information while I was testing it.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class FileFilter
{
public:
	virtual char transform(char ch) = 0;

	void doFilter (ifstream &in, ofstream &out);
};

void FileFilter::doFilter(ifstream &in, ofstream &out)
{
	char ch;
	char transCh;

	in.get(ch);

	while (!in.fail())
	{
		transCh = transform(ch);
		out.put(transCh);
		in.get(ch);
	}
};

class Encryption : public FileFilter
{
private:
	int key;

public:
	char transform(char ch)
	{
		return ch + key;
	}

	Encryption (int encKey)
	{
		key = encKey;
	}
};

class ToUpper : public FileFilter
{
public:
	char transform(char ch)
	{
		return toupper(ch);
	}

};

class Unchanged : public FileFilter
{
public:
	char transform(char ch)
	{
		return ch;
	}
};

class DoubleSpace : public FileFilter
{
public:

	char transform(char ch)
	{
		
	}
};

int main()
{
	ifstream inFile;
	ofstream outFile;
	char inFileName[100], outFileName[100];
	int offset;

	cout << "The four files to be used are called: \nin.txt\nkey.txt\nupper.txt\ncopy.txt\ndouble.txt" << endl;

	cout << "\nEnter file to encrypt: ";
	cin >> inFileName;

	/*
	cout << "Enter file to receive encrypted text: ";
	cin >> outFileName;

	cout << "Enter encryption key: ";
	cin >> offset;
	*/
	inFile.open(inFileName);
	/*outFile.open(outFileName);

	if ((!inFile) || (!outFile))
	{
		cout << "File opening error, close program and try again." << endl;

		system("pause");
		
		exit(1);
	}

	Encryption obfuscate(offset);
	obfuscate.doFilter(inFile, outFile);

	outFile.close();

	inFile.clear();
	inFile.seekg(0L, ios::beg);

	cout << "\nEnter file to receive uppercase text: ";
	cin >> outFileName;

	outFile.open(outFileName);

	while (!outFile)
	{
		cout << "File opening error, please re-enter name: ";
		cin >> outFileName;
	}

	ToUpper upper;

	upper.doFilter(inFile, outFile);

	outFile.close();

	inFile.clear();
	inFile.seekg(0L, ios::beg);

	cout << "\nEnter file to receive untouched copy: ";
	cin >> outFileName;

	outFile.open(outFileName);

	while (!outFile)
	{
		cout << "File opening error, please re-enter name: ";
		cin >> outFileName;
	}
	
	Unchanged copy;

	copy.doFilter(inFile, outFile);

	outFile.close();

	inFile.clear();
	inFile.seekg(0L, ios::beg);
	*/
	cout << "\nFinally, enter file to receive double spaced copy: ";
	cin >> outFileName;

	outFile.open(outFileName);

	while (!outFile)
	{
		cout << "File opening error, please re-enter name: ";
		cin >> outFileName;
	}

	DoubleSpace doubleSpace;

	doubleSpace.doFilter(inFile, outFile);

	inFile.close();
	outFile.close();

	return 0;
}
Apparently you'd have to override the doFilter function, since currently it only supports 1:1 character transformations. Or modify it in the base class to allow arbitrary N:M (or at least 1:N) conversions.

Incidentally, this could be a nice excuse to explore parts of C++ that are often overlooked in school: you could derive your filters from std::codecvt (since std::ofstream already runs each and every character through the virtual function std::codecvt::out, it could as well run them through your override), or you could derive from ostream itself, and provide a different overload of overflow(), similar to how stream filters are done in boost.iostreams
Topic archived. No new replies allowed.