file handling

i am using dev c++ 5.2.0.3
and this code shows no error in compiling but still does not run
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
#include<iostream>

#include<fstream>
using namespace std;
#include<string>
class stu
{
char name;
int roll;
float fee;
public:
	void get()
	{
		cout<<"\nenter your name";
		cin>>name;
		cout<<"\nenter rollno.";
		cin>>roll;
		cout<<"\nenter fee";
		cin>>fee;		
	}
	void show()
	{
		cout<<endl<<name<<endl<<roll<<endl<<fee;
	}
};

int main()
{
stu detail[100];
int i,j,a=-1,b;
for(i=0;i<100;i++)
{
	detail[i].get();
	cout<<"\n\nenter 1 to continue\nenter 2 to exit";
	cin>>a;
	switch(a)
	{
		case 1:
			break;
		case 2:
			goto end2;	
	}
}
end2:
fstream f;
f.open("E:/dev cpp/student1.txt",ios::in|ios::out|ios::ate|ios::binary);
for(j=0;j<i;j++)
{
	f.write((char*)&detail[i],sizeof(detail[0]));
}

cout<<"\n\npress 1 to view\npress 2 to new";
cin>>a;
switch(a)
{
	case 1:
		int c;
		c=f.tellp();
		int d;
		d=sizeof(detail[0]);
		for(i=0;i<(c/d);i++)
		{
			cout<<endl;
			detail[i].show();
			cout<<endl;
		}
	case 2:
		c=f.tellp();
		d=sizeof(detail[0]);
		for(i=c/d;i<100;i++)
		{
			f.seekp(0,ios::end);
			detail[i].get();
			f.write((char *)&detail[i],sizeof(detail[0]));
			cout<<"press 1 to exit";
			cin>>a;
			if(a==1)
			{
				goto end1;
			}
		}
		
}
end1:

	f.close();
return 0;

}


plz tell me where am i wrong????
line 8 should probably be of type string since u probably want a name longer than 1 character. Also note that 'cin' won't work if u try putting in a full name like: "John Deer" because that space between John and Deer is a delimiter. Inputting any bad character at anytime will case 'cin' and thus ur program to crash so there should be plenty of error checking the values typed in by the typical user.

I highly recommend not using keyword goto as well for controlling the flow as it can be incredibly difficult to debug in the future...There are simple fixes for example:
1
2
3
4
5
6
7
8
cin>>a;
	switch(a)
	{
		case 1:
			break;
		case 2:
			goto end2;	   //endl and end1 appear almost identical...
	}


can be:
1
2
3
cin>>a;
if(a == '1')
   break;


line 46: normally u should just open the file for either reading OR for writing, not both. You know better than I what you need but I don't see where u need any of: in, ate, binary flags...maybe app though...

There seems to be another problem with the 2nd half with but I'll let u try to sort it out first...
Last edited on
dev c++ is a no-no, if you want to use something like it I believe several forks are available, like orwells dev c++, the reason is dev c++ hasn't been updated in years, so it doesn't and never will support the latest versions of c++

Code::Blocks is what I would recommend though
the code works well in turbo c3 in windows xp
but it doesnot in dev c++ in windows 7
What do u mean it doesn't work in dev c++ win7 ? Doesn't compile ? Errors ? What do the errors say ?
it says
process could not be attached


normally u should just open the file for either reading OR for writing, not both.


but doesnt fstream allow us to use both.

and could you provide me an example code for file handling for entring data into a file.
Last edited on
but doesnt fstream allow us to use both.

fstream can be used for both but not both at the same moment
what you did is:
f.open("E:/dev cpp/student1.txt",ios::in|ios::out|ios::ate|ios::binary);
but you should remove ios::in OR ios::out
hey it is doing well
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
#include<iostream>
#include<fstream>
using namespace std;
class stu
{
    char name[20];
    int roll;
    float fee;
    public:
    void get()
    {
        cin>>name;
        cin>>roll>>fee;
    }
    void show()
    {
        cout<<endl<<name<<endl<<roll<<endl<<fee<<endl;
    }
};

int main()
{
    int i,a,b,c;
    stu detail[3];
    for(i=0;i<3;i++)
    {
        detail[i].get();
    }
    fstream f;
    f.open("e:\\student2.txt",ios::in|ios::out|ios::ate|ios::binary);
    if(f.is_open())
    {
        cout<<"found";
    }
    for(i=0;i<3;i++)
    {
        f.write((char*) &detail[i],sizeof(detail[0]));
    }
cout<<"*********************************************************************************"<<endl<<"\t\t\tnow print details"<<endl<<"*********************************************"<<endl;
stu detail2[20];
    a=f.tellp();
    f.seekg(0,ios::beg);
    cout<<a;
    b=sizeof(detail2[0]);
    cout<<endl<<b;
    for(i=0;i<a/b;i++)
    {
        f.read((char *) &detail2[i],sizeof(detail2[i]));
    }
    for(i=0;i<a/b;i++)
    {
        detail2[i].show();
    }
f.close();


return 0;
}


and this working
:O
at last
Topic archived. No new replies allowed.