Problem with if statement.

hello friends...
have a look of this code.
1
2
3
4
5
6
7
8
	fstream fout(file,ios::ate|ios::out|ios::in);
	cout<<"\n"<<sizeoffile()<<"\n";
	if(sizeoffile() == 0)
	{
		cout<<"\nNo Record found...\n";
		fout.close();
		return;
	}  


the output is

5

No Record found...


here is my question.
if sizeoffile() function is returning 5
then how (sizeoffile == 0) can be true.
Last edited on
show definition of sizeoffile.
The obvious answer is that there's something about your sizeoffile() function that causes it to return different values on two consecutive calls. Without seeing the definition of the function, however, we can't possibly know what might be causing that.
my program is based on file io operations. this program is about to maintain the records of employees.

as a reply to MikeyBoy i would like to say
"the function 'sizeoffile()' returns the size of file that contains the information about employees.
as you said that function is returning different values for different calls. But there is no input and output operation performed on the file. between two function calls specified in question. Therefore the sizeoffile() is not supposed to return different values.(if i am right )

so as you asked...
here is the code.
definition for sizeoffile() is at the end.

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

using namespace std;

class employee  
{
	private:
		int id;
		float salary;
		char name[9];
	public:
		int getid()	//returns the id assigned to an employee.
		{
			return id;
		}
		void setdata(int ID)	//inputs the data about employees.
		{
			id = ID;
			cout<<"enter the name\n";
			cin>>name;
			cout<<"enter the salary\n";
			cin>>salary;
		}
		void display()	//displays the data
		{
			cout<<"\t"<<id;
			cout<<"\t\t"<<name;
			cout<<"\t\t"<<salary;
			cout<<"\n";
		}
};

void add_employee(); /* adds the new records by opening the file "DataFile.dat" with append mode.*/
void edit_employee();/* edits the existing records by opening the file "DataFile.dat" with ate mode.*/
void delete_employee();	/*deletes the existing records by opening the file "DataFile.dat" with out mode.*/
void display_employee();/*display the data about an employee.*/
void display_all();/*displays the data about all the employees*/
int getID();/* returns the unique id for new employees. */
int sizeoffile();/*returns the size of DataFile.dat in terms of number of employees*/ 

char file[] = {"DataFile.dat"}; //variable having the name of master file.
char metafile[] = {"metadata.dat"};//varable having the name of file that stores the unique id of record that is recently added.

int main()
{
	int ch;
	while(1)
	{
		cout<<"\t\tEmployee Management System\n";
		cout<<"===========================================\n\n";
		cout<<"1:\tDisplay Records of all Employees\n";
		cout<<"2:\tAdd New Employee\n";
		cout<<"3:\tDelete Employee Record\n";
		cout<<"4:\tEdit Eployee Record\n";
		cout<<"5:\tGet Information About An Employee\n";
		cout<<"6:\tExit\n";
		cout<<"===========================================\n\n";
		cout<<"\tEnter Your Choice...\n> ";
		cin>>ch;
		switch(ch)
		{
			case 1:
				display_all();
				cout<<endl;
				system("pause");
				system("cls");
				break;
			case 2:
				add_employee();
				cout<<endl;
				system("pause");
				system("cls");
				break;
			case 3:
				delete_employee();
				cout<<endl;
				system("pause");
				system("cls");
				break;
			case 4:
				edit_employee();
				cout<<endl;
				system("pause");
				system("cls");
				break;
			case 5:
				display_employee();
				cout<<endl;
				system("pause");
				system("cls");
				break;
			case 6:
				exit(0);
				break;
			default:
				cout<<"\nInvalid Input\n";
				system("pause");
				system("cls");
		}
	}
return(0);
}

int getID()		//returns the unique id for new records.
{
	ifstream fin(metafile);
	int value = 0;
	
	if(fin == 0)
	{
		fin.close();
		ofstream fout(metafile);		//this block is executed only for first time when the program is run.
		fout<<0;
		fout.close();
		fin.open(metafile);
	}
	
	fin>>value;		//reads the unique id which is recently assigned to a record.
	fin.close();
	ofstream fout(metafile);
	fout<<value+1;		//writes the value of id after increamenting it by one.
	fout.close();
	return (value+1);
}/*all the above function code reads the value from file then increament it and write back to the file
 as well as returns the value to calling statement.*/

void add_employee()
{
	employee e1;
	fstream fout(file,ios::app|ios::out);
	system("cls");
	e1.setdata(getID());		//calling get id function to get unique id for new record.
	fout.seekp(0,ios::end);
	fout.write((char *)&e1,sizeof(e1));
	cout<<"File Updated Successfuly...\n";
	fout.close();
}

void edit_employee()		//this function is having problem with sizeoffile function.
{
	employee temp;
	
	int id,counter=0;
	int size = sizeoffile();
	system("cls");
	cout<<"\tEnter the Employee ID\n>\t";
	cin>>id;
	fstream fout(file,ios::ate|ios::out|ios::in);
	cout<<"\n"<<sizeoffile()<<"\n";		/*here the function sizeoffile() is returning 
						expected value(ie.no of records in file DataFile.dat*/
	
	if(sizeoffile() == 0)			/*here the function sizeoffile() is returning 0(zero)...?*/
	{
		cout<<"\nNo Record found...\n";
		fout.close();
		return;
	}
	else
	{
		for(int i=0;i<size;i++)		//for loop that traverses all the records in the file DataFile.dat.
		{
			fout.read((char *)&temp,sizeof(temp));
			if(id == temp.getid())		//id == temp.getid() means record that we are looking for is found.
			{
				int size = sizeoffile();
				size = size - sizeof(temp);
				fout.seekp(size,ios::beg);
				temp.setdata(id);		//updating the record.
				fout.write((char *)&temp,sizeof(temp));
				counter = 1;
				break;
			}
		}
	}
	fout.close();
	if(counter==0)
		cout<<"\nRecord Not Found...\n";
	else
		cout<<"\nRecord updated Successfuly...\n";
}

void delete_employee()
{
	employee e1;
	int id,counter=0;
	system("cls");
	cout<<"\nEnter the Employee ID...\n>";
	cin>>id;
	ifstream fin(file);
	ofstream fout("temp.dat");
	int size = sizeoffile();
	
	if(sizeoffile() == 0)
	{
		cout<<"\nNo Record found...\n";
		fin.close();
		return;
	}
	else
	{
		fin.seekg(0,ios::beg);
		for(int i=0;i<size;i++)		/*for loop copies all the records(excluding one that is to be deleted)
						  from DataFile.dat to temp.dat*/
		{
			fin.read((char *)&e1,sizeof(e1));
			if(e1.getid()==id)
			{
				counter = 1;
				continue;
			}
			fout.write((char *)&e1,sizeof(e1));
		}
	}
	fin.close();
	fout.close();
	if(counter == 0)
	{
		cout<<"\nRecord Not Found...\n";
	}
	else
	{
		fout.open(file);
		fin.open("temp.dat");
		fin.seekg(0,ios::beg);
		for(int i = 0;i<size-1;i++)		/*for loop overwriting the contents stored in DataFile.dat
							  with th contents of temp.dat file.*/
		{
			fin.read((char *)&e1,sizeof(e1));
			fout.write((char *)&e1,sizeof(e1));
		}
		cout<<"\nFile Updated Successfuly...\n";
	}
	fin.close();
	fout.close();	
}

void display_employee()		//displays a particular the record
{
	employee e1;
	int id,counter=0;
	int size = sizeoffile();
	system("cls");
	ifstream fin(file);
	if(sizeoffile()==0)  //return if DataFile.dat is empty. here the sizeoffile() is returning expected value.
	{
		cout<<"\nNo Record Found...\n";
		fin.close();
		return;
	}
	else
	{
		cout<<"\nEnter the Employee id...\n>";
		cin>>id;
		for(int i = 0;i<size;i++)
		{
			fin.read((char *)&e1,sizeof(e1));
			if(e1.getid()==id)
			{
				cout<<"\n\tID\tNAME\tSALARY\n";
				cout<<"================================================\n";
				e1.display();
				counter=1;
			}
		}
	}
	fin.close();
	if(counter==0)
		cout<<"\nRecord Not Found...\n";
}

void display_all()
{
	employee e1;
	int size = sizeoffile();
	ifstream fin(file);
	if(sizeoffile()==0)		//sizeoffile again returning expected value.
	{
		cout<<"\nNo Record Found...\n";
		fin.close();
		return;
	}
	cout<<"\n\tID\t\tNAME\t\tSALARY\n";
	cout<<"================================================\n";
	for(int i = 0;i<size;i++)
	{
		fin.read((char *)&e1,sizeof(e1));
		e1.display();
	}
}

int sizeoffile()		//this function returns the size of DataFile.dat in terms of records or objects of employee type.
{
	employee eg;
	int size,bytes;
	ifstream fin(file);
	fin.seekg(0,ios::end);
	bytes = fin.tellg();		//assigning the size of file in terms of Bytes.
	fin.close();
	size = bytes/sizeof(eg);	//converting the filesize from bytes to no. of records(ie. no. of objects of employee type.)
	return size;
}



Note:the function sizeoffile() only creates problem in edit_employee() function.
There are many problems with this code.

I start with yours: you open file for writing, which probably opens it in exclusive mode so others attempts to open it fails. Then you try open it in your sizeoffile function.


Other very important problem: When using binary interface (read/write family of functions) make sure that file is opened in binary mode. Or else you could have problems. For example your sizeoffile function returns wrong value:
http://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file
http://stackoverflow.com/questions/2641639/fstreams-tellg-seekg-returning-higher-value-than-expected
http://stackoverflow.com/questions/27234202/why-fstreamtellg-return-value-is-enlarged-by-the-number-of-newlines-in-the-i

Is the output "No Record Found" or "Record Not Found"? In other words, is the output coming from line 155 or 178?
it is from 155("No Record Found")
Last edited on
In Reply to MiiniPaa

as you told that the function sizeoffile() is not able to access file (probably because of any locking handle due to any previous operation of opening file)

then why it is returning expected value at the line no. 150
and if the function is giving differrent values for different calls.

then what is 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
void edit_employee()		//this function is having problem with sizeoffile function.
{
	employee temp;
	
	int id,counter=0;
	int size = sizeoffile();
	system("cls");
	cout<<"\tEnter the Employee ID\n>\t";
	cin>>id;
	fstream fout(file,ios::ate|ios::out|ios::in);
	cout<<"\n"<<sizeoffile()<<"\n";		/*here the function sizeoffile() is returning 
						expected value(ie.no of records in file DataFile.dat*/
	cout<<"\n"<<sizeoffile()<<"\n";
	cout<<"\n"<<sizeoffile()<<"\n";
	if(sizeoffile == 0)
	{
		cout<<"\nNo Record found...\n";
		fout.close();
		return;
	}
	else
	{
		for(int i=0;i<size;i++)		//for loop that traverses all the records in the file DataFile.dat.
		{
			fout.read((char *)&temp,sizeof(temp));
			if(id == temp.getid())		//id == temp.getid() means record that we are looking for is found.
			{
				int size = sizeoffile();
				size = size - sizeof(temp);
				fout.seekp(size,ios::beg);
				temp.setdata(id);		//updating the record.
				fout.write((char *)&temp,sizeof(temp));
				counter = 1;
				break;
			}
		}
	}
	fout.close();
	if(counter==0)
		cout<<"\nRecord Not Found...\n";
	else
		cout<<"\nRecord updated Successfuly...\n";
}


output for above function is
enter the employee id
10
5
5
5
No Record Found . . .

in the above code the function sizeoffile() is returning same value for three different calls which is expected one.
as i have exactly 5 records in my DataFile.dat

i think the function sizefofile() is fine
Last edited on
i knew it

guys there is nothing wrong with sizeoffile().

see the code below (line no. 13)
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
void edit_employee()		//this function is having problem with sizeoffile function.
{
	employee temp;
	
	int id,counter=0;
	int size = sizeoffile();
	system("cls");
	cout<<"\tEnter the Employee ID\n>\t";
	cin>>id;
	fstream fout(file,ios::ate|ios::out|ios::in);
	cout<<"\n"<<sizeoffile()<<"\n";		/*here the function sizeoffile() is returning 
						expected value(ie.no of records in file DataFile.dat*/
	if(0)
	{
		cout<<"\nNo Record found...\n";
		fout.close();
		return;
	}
	else
	{
		for(int i=0;i<size;i++)		//for loop that traverses all the records in the file DataFile.dat.
		{
			fout.read((char *)&temp,sizeof(temp));
			if(id == temp.getid())		//id == temp.getid() means record that we are looking for is found.
			{
				int size = sizeoffile();
				size = size - sizeof(temp);
				fout.seekp(size,ios::beg);
				temp.setdata(id);		//updating the record.
				fout.write((char *)&temp,sizeof(temp));
				counter = 1;
				break;
			}
		}
	}
	fout.close();
	if(counter==0)
		cout<<"\nRecord Not Found...\n";
	else
		cout<<"\nRecord updated Successfuly...\n";
}


i have replaced the if condition sizeoffile() == 0 with constant 0
again the if condition is being executed and the output is

No Record Found . . .
Last edited on
So it is not the line 15 (in previous post) which gives you this output. As it cannot be called at all.

Where else you have string No Record Found . . . ?
Did you change it in delete_employee() as well?
Change each of the "No Record Found" lines to be something unique like "No Record found in edit_employee", "No record found in display_all" etc.

Better yet, can you run this in a debugger so you can see what it's actually doing?
In reply to dhayden (actualy it also for MiimiPaa and shadowmouse)

i have replaced the the string "No Record Found" with the name of funtion in which it was existing.
for example in edit_employee function i have replaced it with the string "edit_function"
also i have deleted previous .exe .cpp and other files that i have used in my project.
that means now i don't have the string "No Record Found . . ." in the code.(or anywhere else in my computer)

But again when i calls edit_employee to edit a record
then again the output is
No Record Found . . .

also i know the function edit_employee is being executed till before the if condition.
ie. if i writes cout<<"\nedit_employee function executing\n";
just before the if condition then the output is

Enter the Employee Id
1
3
edit_employee function executing
No Record Found . . .


Last edited on
that means now i don't have the string "No Record Found . . ." in the code.(or anywhere else in my computer)

But again when i calls edit_employee to edit a record
then again the output is
No Record Found . . .
How do you build and run your code? You are running old version of your executable so no changes in your code have any effects.
i told that i have deleted .exe file
recompile my program and exected a new created exe file
but again
No Record Found . . .
Have you done a full automated search through all project files for the string No Record Found?
Please post your current full code. Have you tried running it in a debugger? It would be almost trivial to see what's wrong if you did.
Topic archived. No new replies allowed.