Wrong output from file (.txt)

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
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
#include<limits.h>


class customer
{
	char city[21];
	char state[21];
	char name[51];
	char email[81];	
	char username[13];
	char password[13];	
	char question[51];
	char answer[51];

	
	public :	
	
	void getdetails();
	void showdetails();
	
};

void customer::getdetails()
{
	cout<<" Enter Your Full Name (Max 50 chars): ";
	cin.getline(name,51);
	cout<<"\n\n Enter e-mail (Max 80 chars): ";
	cin.getline(email,81);
	cout<<"\n\n Enter City (Max 20 chars ): ";
	cin.getline(city,21);
	cout<<"\n\n Enter State (Max 20 Chars): ";
	cin.getline(state,21);
	cout<<"\n\n Enter Username (Max 12 chars) : ";
	cin.getline(username , 13);
	cout<<"\n\n Password : ";
	cin.getline(password , 13);
	cout<<"\n\n Enter Secret question : ";
	cin.getline(question,51);
	cout<<"\n\n Enter Answer : ";
	cin.getline(answer, 51);

	
	cout<<"\n\n\t\t\t\t Thank You ";
	cout<<"\n\n\t\t\t PRESS ANY KEY TO CONTINUE ";
	getch();
}




void customer::showdetails()
{
	cout<<"\nName  : "; puts(name);
	cout<<"E-mail  : "; puts(email);
	cout<<"City    : "; puts(city);
	cout<<"State   : "; puts(state);
	
	cout<<"Username : "; puts(username);
	cout<<"Password : "; puts(password);
	cout<<"Question : "; puts(question);
	cout<<"Answer : ";   puts(answer);
}



void main()
{
	clrscr();		
	customer obj;
	int ch;
	
	cout<<" 1. Enter details  ";
	cout<<" 2. View Details  ";
	cin>>ch;
	cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n');
	if(ch==1)
	{
		clrscr();
		obj.getdetails();
		clrscr();
	
		cout<<"Here is what you entered "<<endl<<endl;
		obj.showdetails();	
		getch();	
		ofstream abc;
		abc.open("customer.txt" , ios::app);
		abc.write( (char*)&obj , sizeof(obj) );
		
	}
	
	if(ch==2)
	{
		ifstream abc;
		abc.open("customer.txt" , ios::in);
		abc.seekg(0); // to go to beggining of the file
		while(abc)	// to read the whole file
		{
			abc.read( (char*)&obj , sizeof(obj) );
			cout<<"\n\nDetails of object";
			obj.showdetails();		
		}
			abc.close();
			
	}
	
	
	getch();
			
}	


The main objective of the program is to accept user details and store them in a file for later viewing

Unfortunately the above code is not doing that. After i have entered the details, notice that i am calling the obj.showdetails() function to show me what's going into the file. Everything is fine till there.

After i choose option 2 (View Details ) i see a whole lot of garbage. Many of the details i have entered for ex. e-mail , state are replaced with wierd symbols.

For ex:
If i entered e-mail as : email@email.com
On choosing option 2 it does not show me emai@email.com , instead some wird symbols show up (like an arrow or a blank ).

Can anyone explain why is this happening ?


Note : I am using Borland 5.5 compiler
Last edited on
To read the whole file use while(!abc.eof()), not while(abc).

abc.write( (char*)&obj , sizeof(obj) ); and
abc.read( (char*)&obj , sizeof(obj) );
should be changed in
1
2
abc << obj.name << '\n' << obj.email << '\n' << obj.city << '\n' << obj.state << '\n' << obj.username 
     << '\n' << obj.password << '\n' << obj.question << '\n' << obj.answer << '\n';

and
abc >> obj.name >> obj.email >> obj.city >> obj.state >> obj.username >> obj.password >> obj.question >> obj.answer;
but for this you have to daclare those member as public.
My suggestion is to create a member function to do this.
Last edited on
Thanks Bazzy !
But i have one more problem. I switched to while( !abc.eof() ) instead of while(abc). When displaying the records from the file , the last record gets displayed twice
For Ex :
1
2
3
4
5
6
7
8
9
10
11
12
13
14

void customer :: readfromfile()
{
	ifstream abc;
	abc.open("customer.txt" , ios::in);
	
	abc.seekg(0); // to go to beggining of the file
	while( !abc.eof() )	// to read the whole file
	{
			abc>>name>>email>>city>>state>>username>>password>>question>>answer;
			showdetails();		
	}
			abc.close();
}


After executing this member function. The last set of details get displayed twice. Any solution to that ?

Once again thanks a lot Bazzy.
I don't get that error, could you show me your whole code?
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
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
#include<limits.h>


class customer
{
	char city[21];
	char state[21];
	char name[51];
	char email[81];	
	char username[13];
	char password[13];	
	char question[51];
	char answer[51];

	
	public :	
	
	void getdetails();
	void showdetails();
	void writetofile();
	void readfromfile();
	
};

void customer::getdetails()
{
	cout<<" Enter Your Full Name (Max 50 chars): ";
	cin.getline(name,51);
	cout<<"\n\n Enter e-mail (Max 80 chars): ";
	cin.getline(email,81);
	cout<<"\n\n Enter City (Max 20 chars ): ";
	cin.getline(city,21);
	cout<<"\n\n Enter State (Max 20 Chars): ";
	cin.getline(state,21);
	cout<<"\n\n Enter Username (Max 12 chars) : ";
	cin.getline(username , 13);
	cout<<"\n\n Password : ";
	cin.getline(password , 13);
	cout<<"\n\n Enter Secret question : ";
	cin.getline(question,51);
	cout<<"\n\n Enter Answer : ";
	cin.getline(answer, 51);

	
	cout<<"\n\n\t\t\t\t Thank You ";
	cout<<"\n\n\t\t\t PRESS ANY KEY TO CONTINUE ";
	getch();
}




void customer::showdetails()
{
	cout<<"\nName  : "; puts(name);
	cout<<"E-mail  : "; puts(email);
	cout<<"City    : "; puts(city);
	cout<<"State   : "; puts(state);
	
	cout<<"Username : "; puts(username);
	cout<<"Password : "; puts(password);
	cout<<"Question : "; puts(question);
	cout<<"Answer : ";   puts(answer);
}

void customer :: writetofile()
{
	
	ofstream abc;
	abc.open("customer.txt" , ios::app);
	
	abc<<name<<'\n'<<email<<'\n'<<city<<'\n'<<state<<'\n'<<username<<'\n'<<password<<'\n'
		<<question<<'\n'<<answer<<'\n';
	abc.close();
	
}

void customer :: readfromfile()
{
	ifstream abc;
	abc.open("customer.txt" , ios::in);
	
	abc.seekg(0); // to go to beggining of the file
	while( !abc.eof() )	// to read the whole file
	{
			abc>>name>>email>>city>>state>>username>>password>>question>>answer;
			showdetails();		
	}
			abc.close();
}

void main()
{
	clrscr();		
	customer obj;
	int ch;
	
	cout<<" 1. Enter details  ";
	cout<<" 2. View Details  ";
	cin>>ch;
	cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n');
	if(ch==1)
	{
		clrscr();
		obj.getdetails();
		clrscr();
	
		cout<<"Here is what you entered "<<endl<<endl;
		obj.showdetails();	
		getch();	
		
		obj.writetofile();
	}
	
	if(ch==2)
	{

			obj.readfromfile();	

			
	}
	
	
	getch();
			
}	



Here is the code.

I found out another bug.

If any of the variables of the class have a space in the value typed by the user (for ex: Name : Michael Faraday ) it writes the word before the space ('Michael' - in this example ) into the variable 'name' and the next word after the space ('Faraday' ) into the next variable 'email'.

So the output from the file is not correct.

Just check the program out by typing 2 words for a variable (like 'abc abc')


Last edited on
Ok Bazzy. I fixed a part of the bug myself :

here is the updated function : Just replace this with the old fuction in my above post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void customer :: readfromfile()
{
	ifstream abc;
	abc.open("customer.txt" , ios::in);
	
	abc.seekg(0); // to go to beggining of the file
	while( !abc.eof() )	// to read the whole file
	{
			//abc>>name>>email>>city>>state>>username>>password>>question>>answer;
			
			abc.getline(name,51);
			abc.getline(email,81);
			abc.getline(city,21);
			abc.getline(state,21);
			abc.getline(username,13);
			abc.getline(password,13);
			abc.getline(question,51);
			abc.getline(answer,51);
			
			showdetails();		
	}
			abc.close();
}


I can now accept spaces and read them from the file properly and display them too.

Moreover with this program - the last record is displayed only once but - after the last record a blank record is displayed

For Ex: This is the output when i choose option 2
=======================================

Name : Michael Faraday
E-mail : Michael_Faraday@faraday.com
City : heaven
State : heaven
Username : Michael F.
Password : Michael
Question : question
Answer : answer

Name:
E-mail:
City:
State:
Username:
Password:
Question:
Answer:

========================================

Any idea how to debug this ?

Last edited on
It is probably because there is an extra line on the file, and since your while loop is only checking for eof, it will try to getline 10 times, which all get nothing (since the eof is reached after the first getline).
Thx a lot firedraco. Problem Solved !

1
2
	abc<<name<<'\n'<<email<<'\n'<<city<<'\n'<<state<<'\n'<<username<<'\n'<<password<<'\n'
		<<question<<'\n'<<answer<<'\n';


There lied my problem. The '\n' character after answer.
Last edited on
Topic archived. No new replies allowed.