Reading/writing text/binary

I have to create a class which lets me write and read an int and a double to a text file, and a binary file.

I keep getting a compiling error that's telling me to look at my class declaration, I have no idea whats wrong because this looks right to me

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class File
{
public: int x = 0;
		double y = 0;
private:
	File()
	{
		x = 100;
		y = x*x;
	}
	void writeInt(const string name);
	void writeDouble(const string name);
	void printText(const string name);
	void printBinary(const string name);
	~File();
};


void File ::writeInt(string name)
{
	char file;
	cout << "Press (T/B) to write in either text or binary files: " << endl;
	cin >> file;
	if (file == 'T' || 't')
	{
		ofstream f;
		f.open(name);
		f << x << endl;
		f.close();
	}
	else if (file == 'b' || 'B')
	{
		ofstream b;
		b.open(name, ios::out | ios::binary);
		b.write((char*)&x, sizeof(x));
		b.close;
	}
	else
		cout << "Invalid option" << endl;

}

void File::writeDouble(string name)
{
	char file;
	cout << "Press (T/B) to write in either text or binary files: " << endl;
	cin >> file;
	if (file == 'T' || 't')
	{
		ofstream f;
		f.open(name);
		f << y << endl;
		f.close();
	}
	else if (file == 'b' || 'B')
	{
		ofstream b;
		b.open(name, ios::out | ios::binary | ios::ate);
		b.write((char*)&y, sizeof(y));
		b.close;
	}
	else
		cout << "Invalid option" << endl;
}

void File::printText(string name)
{
	string read;
	ifstream r;
	if (r.is_open())
	{
		while (getline(r, read)) //getline extracts chars from r, stores into read
		{
			cout << read << "\n";
		}
		r.close();
	}
	else
		cout << "Error in opening file!" << endl;

}

void File::printBinary(string name)
{
	streampos size;
	char *mem;

	ifstream bin(name, ios::in | ios::binary);
	if (bin.is_open())
	{
		size = bin.tellg();
		mem = new char[size];
		bin.seekg(0, ios::beg);
		bin.read(mem, size);

		bin.close();
		delete[] mem;
	}
}

int main()
{
	File Text, Binary;
	const string file = "Text.txt";
	const string binary = "Bin.bin"; 

	//write Text
	Text.writeInt(file);
	Text.writeDouble(file);

	//write Binary
	Binary.writeInt(binary);
	Binary.writeDouble(binary);

	system("PAUSE");
	return 0;

}
You've got your access specifiers the wrong way round (functions should in general be public and member variables private) and you should define your destructor as either {} or =default;. Also b.close; is invalid, all functions are invoked with ().
Last edited on
Could you give the error message? Usually it tells you what is wrong.

In any case, lines 30, 37, 54, and 61 have bad conditions, as 't' or 'B' will always evaluate to true, resulting in the entire condition always being true.
Topic archived. No new replies allowed.