Need help eliminating errors from this program

I have written a menu driven program that has file handling in it. It shows particular error with file handing. I have declared an object 'f' of fstream class but the compiler says "fstream does name a type".

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
/*Aim-To define a class named 'student',accept,display,search by rollno,search by name,modify by usinf file handling*/
#include<fstream>
#include<iostream>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
void readfile();
void writefile();
void modify(int);
void search(int);
void search(char *);

class student
{private:
int rollno;
char name[20];

public:
void getdata();
void putdata();
int retrollno()
{return rollno;}
char *retname()
{return name;}
}s;
fstream f;

void writefile()
{f.open("Stud.dat",ios::binary|ios::app);
cout<<"Enter data\n";
s.getdata();
f.write((char *)&s,sizeof(s));
f.close();}

void student::getdata()
{cout<<"Enter name\n";
cin>>name;
cout<<"Enter roll no\n";
cin>>rollno;}

void readfile()
{f.open("Stud.dat",ios::binary|ios::in);

while(f.read((char *)&s,sizeof(s)))
s.putdata();
f.close();}

void student::putdata()
{cout<<"Roll no - "<<rollno<<"\n";
cout<<"Name - "<<name<<"\n";}

fstream f1;

void modify(int val)
{
f.open("Stud.dat",ios::binary|ios::in);
f1.open("Temp.dat",ios::binary|ios::out);
while(f.read((char *)&s,sizeof(student)))
    {if(s.retrollno()==val)
	{cout<<"Found\n";
	cout<<"Enter new data\n";
	s.getdata();}
	f1.write((char *)&s,sizeof(student));
	}
f.close();
f1.close();
remove("Stud.dat");
rename("Temp.dat","Stud.dat");}

void search(int val)
{int flag=0;
f.open("Stud.dat",ios::binary|ios::in);
while(f.read((char *)&s,sizeof(student)))
{   if(s.retrollno()==val)
	{cout<<"Found\n";
	flag=1;
	s.putdata();
	break;}}
    f.close();
if(flag==0)
cout<<"Not found \n";}

void search(char *n)
{int flag=0;
f.open("Stud.dat",ios::binary|ios::in);
while(f.read((char *)&s,sizeof(student)))
{if(strcmpi(s.retname(),n)==0)
    {
	cout<<"Found\n";
	s.putdata();
	flag=1;
	break;
    }
}
f.close();
if(flag==0)
{
    cout<<"Not found\n";
}
}
int main()
{int choice,n;
char ch,tempname[20];
do
{
cout<<"1)Enter data\n";
cout<<"2)Display data\n";
cout<<"3)Search by roll no\n";
cout<<"4)Search by name\n";
cout<<"5)Modify\n";
cout<<"6)Exit\n";
cout<<"Enter choice\n";
cin>>choice;
switch(choice)
{
    case 1:
    writefile();
    break;

    case 2:
    readfile();
    break;

    case 3:
    cout<<"Enter roll no to search\n";
    cin>>n;
    search(n);
    break;

    case 4:
    cout<<"Enter name to search\n";
    cin>>tempname;
    search(tempname);
    break;

    case 5:
    cout<<"Enter roll no to search\n";
    cin>>n;
    modify(n);
    break;

    case 6:
    exit(0);
    break;
}
cout<<"Do you want to continue?\n";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
return 0;
}



Edit 1: I edited the program and eliminated many errors. But there are still 2 errors:
1)In line 65, "|65|error: no matching function for call to 'std::basic_fstream<char, std::char_traits<char> >::close(const char [9])'| "
2)In line 66, "|66|error: no matching function for call to 'std::basic_fstream<char, std::char_traits<char> >::close(const char [9])'| "

Edit 2: I finally eliminated all the errors in the program. In line 65 and line 66 , there was no need to provide the file names.

Edit 3: I again edited this program and it is working but somehow whatever
I input is not saved in the file "Stud.dat". What should I do?
Last edited on
You should either use nested name with namespace std as for example

std::fstream f;

or include using deirective

using namespace std;

in your code.
Topic archived. No new replies allowed.