need help with fstream

I written this entire code, however I can't figure out how this line is not working. fstream f("encoded.txt", f.out | f.app);. It tells me that f is undefined. Please help.

1
2
3
4
5
6
7
8
9
10
11
	case 'S':
	{

		cout << "Enter the encoding level <0, 1, 2, or 3>" << endl;
		cin >> level;
		cout << "Enter the file name" << endl;
		cin >> filename;
		fstream f("encoded.txt", f.out | f.app);
		switch (level) {

		case 1:
Show a complete example that we can paste and attempt to compile.

Did you #include <fstream>?
Last edited on
yes i did.

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <ctype.h>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cstring>
using namespace std;

// Define a constant for the number of lines to read
#define NUM_READ_LINES 11
char option;
int option2;
int decr = 3;
int incr = 5;
int inc2 = 4;
int inc3 = 3;
int temp;
int first_al, second_al;
string description, username, password, notes;
char thearray[NUM_READ_LINES][200];
string new_string = "";
char showOptions()
{

	cout << "\n" << endl;
	cout << "\t" << "AVAILABLE OPTIONS" << "\n" << endl;
	cout << "D - DISPLAY LINE DESCRIPTIONS\n";
	cout << "V - VIEW LINE DATA\n";
	cout << "E - EDIT LINE DATA\n";
	cout << "A - ADD LINE DATA\n";
	cout << "S - SAVE AND ENCODE FILE\n";
	cout << "X - EXIT PROGRAM\n";
	cin >> option;
}

int viewDetails(string opt)
{
	cout << "Enter line number you wish to " << opt << "\n";

	cin >> option2;

	std::ostringstream ss;
	ss << thearray[option2];
	//cout<<thearray[option2];
	int strpos = ss.str().find(";");

	std::string s = ss.str();
	std::string delimiter = ";";

	size_t pos = 0;
	int i = 1;
	std::string token;
	//here you display the token after getting a substring
	//after displaying the substring, it is erased from memory to form a new string wthout the first sustring

	while ((pos = s.find(delimiter)) != std::string::npos) {
		token = s.substr(0, pos);
		if (i == 1)
			std::cout << "Row Desc\t" << token << std::endl;
		if (i == 2)
			std::cout << "Username\t" << token << std::endl;
		if (i == 3)
			std::cout << "Password\t" << token << std::endl;
		if (i == 4)
			std::cout << "Notes\t" << token << std::endl;
		s.erase(0, pos + delimiter.length());
		i += 1;
	}

	return option2;

}

int main() {
	int level;


	// Array of line numbers each line being no more than 200 chars

	int counter = 0;
	std::string filename;
	cout << "Enter the name of the file\n";
	cin >> filename;

	// Open your file
	ifstream inFile("pwTest.txt", ifstream::in);

	// If we can read/write great
	if (inFile.good()) {

		// Read throuthe file and load into array
		while (!inFile.eof()) {
			inFile.getline(thearray[counter], 600);
			counter++;
		}



	}
	//here we convert the array elements to string and then display the description of each data
	for (int i = 0; i <= counter; i++) {
		std::ostringstream ss;
		ss << thearray[i];
		int strpos = ss.str().find(";");//this is to be used in splitting the array element
		if (i == 0)
			continue;
		cout << i << "\t" << ss.str().substr(0, strpos) << endl;//the array element is split and the description displayed

	}

	inFile.close();

	showOptions();
	//cin>>option;


	switch (toupper(option))
	{
	case 'D':
		for (int i = 0; i <= counter; i++) {
			std::ostringstream ss;
			ss << thearray[i];
			int strpos = ss.str().find(";");
			if (i == 0)
				continue;
			cout << i << "\t" << ss.str().substr(0, strpos) << endl;

		}
		showOptions();
		break;
	case 'V':
		viewDetails("view");
		break;
		//this is for editing the line data
	case 'E':
		viewDetails("edit");
		cout << "WARNING:  You cannot use semi-colons in these fields. Any semi-colons entered will be removed\n" << endl;
		cout << "Enter a line description:\n";
		cin >> description;
		description.append(";");
		cout << "Enter a username\n";
		cin >> username;
		description.append(username + ";");



		cout << "Enter a password\n";
		cin >> password;
		description.append(password + ";");


		cout << "Enter notes\n";
		cin >> notes;
		description.append(notes + ";");
		char cstr[sizeof(description) + 1];
		strcpy(cstr, description.c_str());
		cout << cstr;
		//thearray[option2]=cstr;
		cout << "Add security question";
		cin >> new_string;

		char choice;
		cout << "Are you sure you want to edit the data... Y/N";
		cin >> choice;


		showOptions();

		break;
	case 'X':
		cout << "Exiting system....\n";
		return 0;
		break;
		//savng and encoding file
	case 'S':
	{

		cout << "Enter the encoding level <0, 1, 2, or 3>" << endl;
		cin >> level;
		cout << "Enter the file name" << endl;
		cin >> filename;
		fstream f("encoded.txt", f.out | f.app);
		switch (level) {

		case 1:
		{
			f << "E1" << endl;
			std::ifstream file("pwTest.txt");

			if (file.is_open()) {
				std::string line;
				while (getline(file, line)) {
					cout << line.c_str();
					for (int i = 0; i < line.size(); i++) {
						if (int(line.c_str()[i]) != 59) {
							first_al = (int(line.c_str()[i])) - int(decr);
							//cout<<first_al<<endl;
							if (i % 2 != 0)
							{
								second_al = first_al + incr;
								f << second_al;
							}
							f << first_al;

						}
						else
						{
							f << ";";
							if (i == 4)
								f << endl;
						}


					}
				}
				file.close();
			}
			else
				cout << "Bad" << endl;
			return 0;
		}

		case 2:
		{
			f << "E2" << endl;
			std::ifstream file("pwTest.txt");

			if (file.is_open()) {
				std::string line;
				while (getline(file, line)) {
					cout << line.c_str();
					for (int i = 0; i < line.size(); i++) {
						if (int(line.c_str()[i]) != 59) {
							first_al = (int(line.c_str()[i])) - int(inc2);
							//cout<<first_al<<endl;
							if (i % 3 == 0)
							{
								second_al = first_al + inc3;
								f << second_al;
							}
							else
								f << first_al;
						}
						else
						{
							f << ";";
							if (i == 4)
								f << endl;
						}


					}
				}
				file.close();
			}
			return 0;
		}

		default:
			break;

		}
		return 0;

	}





	case 'A':
		fstream f("pwTest.txt", f.out | f.app);
		cout << "Enter a line description:\n";
		cin >> description;

		cout << "\n";
		cout << "Enter a username\n";
		cin >> username;

		cout << "\n";
		cout << "Enter a password\n";
		cin >> password;

		cout << "\n";
		cout << "Enter notes\n";
		cin >> notes;
		cout << "Are you sure you want to add the data...Y/n?\n";
		cin >> choice;
		switch (toupper(choice))
		{
		case 'Y':
			f << description << ";";
			f << username << ";";
			f << password << ";";
			f << notes << ";";
			break;

		default:
			break;

		}
		showOptions();
		break;




	}


	return 0;
}
lines 185 and 275
Your code compiles for me (but I didn't run it).

What is the exact error message you are getting?
It is a compiler error, link error, run-time error, or (if Visual Studio/Eclipse) an 'intellisense' error?

Your code does have some warnings, though. The first warning will probably crash your program at runtime
 In function 'char showOptions()':
37:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'int viewDetails(std::string)':
48:6: warning: unused variable 'strpos' [-Wunused-variable]
 In function 'int main()':
197:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
235:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 
Last edited on
I'm running visual studio, it says "error C2065: 'f': undeclared identifier".
Okay I opened your code with visual studio. I see the issue. It was compiling for me with g++, but it might be an extension that allowed it to compile -- to treat non-static access to static members as legal.

Change
fstream f("encoded.txt", f.out | f.app);
to
fstream f("encoded.txt", fstream::out | fstream::app);

In hindsight, I should've realized this sooner...
Last edited on
Thank you. Also the error in line 37 is not letting me run it. What should I be returning?
You don't return anything from your function showOptions.

You're using global variables, which is bad practice. But the simplest "fix" would be to change the char showOptions to void showOptions.

Otherwise, you want to add return option;
Last edited on
Topic archived. No new replies allowed.