Not Writing to file.

It does not write to the file for some reason. So when I read it from the file nothing actually shows up. I am stumped on what to do. Any suggestions?

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
#include<iostream>
#include<conio.h>
#include<process.h>
#include<fstream>
using namespace std;
class Bank
{
	int acno;
	char name[20];
	char actype[20];
	double balance;
public:
	void display()
	{
cout << "\nAccount Number: " << acno << "\nName: " << name << "\nAccount Type: " << actype << "\nCurrent Balance: " << balance;
	}
	void input()
	{
		char b;

		cout << "Enter the following details\nAccount Number: ";
		cin >> acno;
		cout << "Name of Owner: ";
		cin.get(b);
		cin.getline(name, 20);
		cout << "Account Type: ";
		cin.getline(actype, 20);
		cout << "Current Balance: ";
		cin >> balance;
	}
}obj, obj2;

void main()
{
	fstream file("bank.txt", ios::app | ios::in);
	while (1)
	{
		char ch;
		cout << "\n1. Insert Record\n2. Display all\n3. Exit";
		cout << "\nEnter your choice: ";
		cin >> ch;
		switch (ch)
		{
		case '1':
		{
					char n = 'y';
					while ((n == 'y' || n == 'Y'))
					{
						obj.input();
						file.write((char *)&obj, sizeof(obj));
						cout << "\nDo you want to add more?(y/n)";
						cin >> n;
					}
					break;
		}
		case '2':
		{
					file.clear();
					file.seekg(0,ios::beg);
					cout << endl << file.tellg();
					while (file.peek()!=EOF)
					{
						file.read((char *)&obj2, sizeof(obj2));
					}
					obj2.display();
					break;
		}
		case '3':
		{
					file.close();
					exit(0);
		}
		default:
			cerr << "Error. Wrong Choice.";
		}
	}
}
Last edited on
Read this:

http://www.cplusplus.com/reference/fstream/fstream/open/

If the mode has both trunc and app set, the opening operation fails. It also fails if either is set but out is not, or if both app and in are set.
On line 35: out is missing

you should check if the file could be opened and print an according message.
Furthermore: you have a relative path. Do you know where the file is?
Hey thanks for answering, I tried it with
 
fstream file("bank.txt", ios::app | ios::in|ios::out);

And now it writes to the file. So hey that works. Thank you. Problem Solved.
Will this code also work if I put this:
 
fstream file("bank.dat", ios::app | ios::in|ios::out|ios::binary);
Will this code also work if I put this:
Yes. Be aware that the binary data must not contain pointer in any form
Thanks again.
It works with TurboC++ but when I tried the original code (i.e fstream file("bank.txt", ios::app | ios::in|ios::out); ) with GCC compiler on Code::Blocks and I keep getting the same errors everytime, basically I can only write once, and after that Whenever I try to write It does not write. It's odd.
Edit:Solved. I tried as you suggested a giving the full file path instead of just a relative path. Thanks
Last edited on
Topic archived. No new replies allowed.