How can i write data in to file nicely???

i'm do my assignment and i have a bit confuse in writting to file method.This is my Uncomplete 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
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
#include<fstream.h>
#include<stdlib.h>

class variable
{
	protected:
		struct student
		{
			char name[100],course[100],id[10],gender;
			int  dob,mob,yob,age,ic;
			double cgpa,marks[10];
		}std[100];
		int i;
};

class student:public variable
{
	private:
	public:
		void reg()
		{
			i=1;
			std[i].dob=std[i].mob=std[i].yob=0;
			cout<<"Enter Student Name   : ";
			cin.ignore();
			cin.getline(std[i].name,100);
			cout<<"Enter Student IC     : ";
			cin.ignore();
			cin>>std[i].ic;
			cout<<"Enter Student Gender : ";
			cin.ignore();
			cin>>std[i].gender;
			cout<<"Enter Student Course : ";
			cin.ignore();
			cin.get(std[i].course,100);
			cout<<"Student Borthday     : "<<endl;;
			cout<<"Date  : ";
			cin.ignore();
			cin>>std[i].dob;
			cout<<"Month : ";
			cin.ignore();
			cin>>std[i].mob;
			cout<<"Year  : ";
			cin.ignore();
			cin>>std[i].yob;
			i++;
		}
		void dpl()
		{
			for(int a=1;a<=i;a++)
			{
					cout<<"Student Name     : "<<std[a].name<<endl;
					cout<<"Student ID       : "<<std[a].id<<endl;
					cout<<"Student IC       : "<<std[a].ic<<endl;
					cout<<"Student Gender   : "<<std[a].gender<<endl;
					cout<<"Student Course   : "<<std[a].course<<endl;
					cout<<"Student Birthday : "<<std[a].dob<<"/"<<std[a].mob<<"/"<<std[a].yob<<endl;
					cout<<"============================================="<<endl;
			}
		}
};

class exam:public student
{

};
class slt_menu:public exam
{
	private:
		int choice;
		char reply;
	public:
		void mainmenu()
		{
			cout<<"\t\t\t*******************************************"<<endl;
			cout<<"\t\t\t*           Extreme Technology            *"<<endl;
			cout<<"\t\t\t*                 Collage                 *"<<endl;
			cout<<"\t\t\t*          Student Detail System          *"<<endl;
			cout<<"\t\t\t*******************************************"<<endl;
			cout<<"\n\t\t\t1.Student Data System"<<endl;
			cout<<"\t\t\t2.Student Examination System"<<endl;
			cout<<"\t\t\t3.Exit From This Program"<<endl;
			cout<<"\n\t\t\tEnter Your Choice : ";
			do{
			cin>>choice;
				switch(choice)
				{
					case 1:
					{
						smenu();
						break;
					}
					case 2:
					{
						break;
					}
					case 3:
					{
						exit(1);
						break;
					}
					default:
					{
						cout<<"\t\t\tYou Have Enter Invalid Input!!!Enter Again!!!"<<endl;
						cout<<"\t\t\tEnter Your Choice Again : ";
					}
				}
			}
			while(choice!=1 || choice !=2 ||choice !=3);
		}

		void smenu()
		{
			student st;
			cout<<"\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
			cout<<"\t\t\t~               Student Data              ~"<<endl;
			cout<<"\t\t\t~                  System                 ~"<<endl;        
			cout<<"\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
			cout<<"\n\t\t\t1.Student Registration"<<endl;
			cout<<"\t\t\t2.Display Student Detail"<<endl;
			cout<<"\t\t\t3.Seacrh  Student Detail"<<endl;
			cout<<"\t\t\t4.Return To Main Menu"<<endl;
			cout<<"\n\t\t\tEnter YOur Choice : ";
			cin>>choice;
			do{	
				switch(choice)
				{
					case 1:
					{
						do{
							reg();
							cout<<"Do You Wish To Add More Data??"<<endl;
							cin>>reply;
						}while(reply=='Y'||reply=='y');{}
						cout<<"Do You Want To Save & Return To Main Menu"<<endl;
						cin>>reply;
						if(reply=='Y'||reply=='y')
						{
							ofstream input("student_details.txt");
							input.write((char*)&st,sizeof(student));
							input.close();
							mainmenu();
						}		
						break;
					}
					case 2:
					{
						ifstream toread("student_details.txt");
						toread.read((char*)&st,sizeof(student));
						while(toread)
						{
							toread.read((char*)&st,sizeof(student));
						}
						toread.close();
						dpl();
					}
					case 3:
					{
						
					}
					case 4:
					{
						mainmenu();
					}
					default:
					{
						cout<<"\t\t\tYou Have Enter Invalid Input!!!Enter Again!!!"<<endl;
						cout<<"\t\t\tEnter Your Choice Again : ";
						cin>>choice;
					}
				}
			}while(choice!=1||choice!=2||choice!=3||choice!=4);
		}
};
void main()
{
	slt_menu m;
	m.mainmenu();
}



how can i write those input by user in to file nicely??The display always can't print out correctly
Last edited on
You will need to be a bit more specific - what is the current output, and what would you like it to be?
ok, i would like to let user to input the detail that in the function as below :
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
void reg()
{
	i=1;
	std[i].dob=std[i].mob=std[i].yob=0;
	cout<<"Enter Student Name   : ";
	cin.ignore();
	cin.getline(std[i].name,100);
	cout<<"Enter Student IC     : ";
	cin.ignore();
	cin>>std[i].ic;
	cout<<"Enter Student Gender : ";
	cin.ignore();
	cin>>std[i].gender;
	cout<<"Enter Student Course : ";
	cin.ignore();
	cin.get(std[i].course,100);
	cout<<"Student Borthday     : "<<endl;;
	cout<<"Date  : ";
	cin.ignore();
	cin>>std[i].dob;
	cout<<"Month : ";
	cin.ignore();
	cin>>std[i].mob;
	cout<<"Year  : ";
	cin.ignore();
	cin>>std[i].yob;
	i++;
}

i need this all data write in a file call "student_details.txt".
and it will display when user want to see the data in fuction as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
void dpl()
{
	for(int a=1;a<=i;a++)
	{
		cout<<"Student Name     : "<<std[a].name<<endl;
		cout<<"Student ID       : "<<std[a].id<<endl;
		cout<<"Student IC       : "<<std[a].ic<<endl;
		cout<<"Student Gender   : "<<std[a].gender<<endl;
		cout<<"Student Course   : "<<std[a].course<<endl;
		cout<<"Student Birthday : "<<std[a].dob<<"/"<<std[a].mob<<"/"<<std[a].yob<<endl;
				cout<<"============================================="<<endl;
	}
}
.
Last edited on
with this code you save all the students in std to file in one move, but over-write any previous data, you can use to initialize the file then to add more as you create them use addtofile()


fstream fp; //declare in class variable(protected area) or gloval variable for later use.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void savetofile(student std[],int a) // where a is the amount of students in std
{

  fp.open("student_details.txt",ios::binary|ios::out); // creates file called student_details.txt

  if(!fp)
    {
        cout<<"Error. Could not create file."<<endl; //failed to create the file.
        exit();                                     // use your exit algorithm. 
    }


   fp.write((char*)std,a*sizeof(student));// save data to file overwrite any previous data.

   fp.close();
}



so its goes:

1
2
3

    savetofile(std,i);// save to file 


check this link, some more info, open, read, and write to file.

http://www.cplusplus.com/forum/general/6320/
Last edited on
if you want to add one at a time to the file, without over-writing previous data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void addtofile(student temp)
 {
   fp.open("student_details.txt",ios::binary|ios::app);//open file.
    if(!fp)
    {
        cout<<"failed to open.!!";
        exit();        //your exit code.
    }
  
   fp.write((char*)&temp,sizeof(student))//writes an the end of file, dont over-write previous data.
 
   fp.close();

}




1
2
3
4
student example;

addtofile(example);  // student *example* copied to the end of file.
and to display from the file: test them all and let me know if they work.

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

void dpl()
{

  int a=0;
  student c;

  fp.open("student_details.txt",ios::binary|ios::in);  //open file
    if(!fp)
     {
        cout<<endl<<"Couldn't read the file. !!!"<<endl; //error
        exit(1);                            //exit code here.
     }

   while(fp.good())
     {
        fp.read((char*)&c,sizeof(student));  //copy to temporal c
        std[a]=c;                                      // then asigned to std in a
   
	cout<<"Student Name     : "<<std[a].name<<endl;
	cout<<"Student ID       : "<<std[a].id<<endl;
	cout<<"Student IC       : "<<std[a].ic<<endl;
	cout<<"Student Gender   : "<<std[a].gender<<endl;
	cout<<"Student Course   : "<<std[a].course<<endl;
	cout<<"Student Birthday : "<<std[a].dob<<"/"<<std[a].mob<<"/"<<std[a].yob<<endl;
	cout<<"============================================="<<endl;        

        a++;
     }

    fp.close();
}
Last edited on
You also want to read http://www.cplusplus.com/forum/articles/6046/ so your not using cin >> value as this will cause you alot of issues and excess code.
thx ^^ ,but if me dun wan to overwrite the previous data that i have input, i using writting this code in my class :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void addtofile(student temp)
 {
   fp.open("student_details.txt",ios::binary|ios::app);//open file.
    if(!fp)
    {
        cout<<"failed to open.!!";
        exit();        //your exit code.
    }
  
   fp.write((char*)&temp,sizeof(student))//writes an the end of file, dont over-write previous data.
 
   fp.close();

}


when i wan to use this function in void main, how should i write ???
Last edited on
implement the fuction in the student class, after/before dpl(), then:

in the void smenu() is where you use the addtofile(), as follow:

when the user select the option 1(case 1) then you prompt if he wants to save, if so then save to file, for this, replace:

1
2
3
4
5
6
7
8
9
10

  if(reply=='Y'||reply=='y')
    {
        ofstream input("student_details.txt");      //
        input.write((char*)&st,sizeof(student));   //replace these three lines with addtofile()
        input.close();                            //
       
      mainmenu();
    }



so its goes like:

1
2
3
4
5
6
7
8

  if(reply=='Y'||reply=='y')
    {
          addtofile(st);     //save the student st to file.
  
       mainmenu();
    }
Last edited on
Topic archived. No new replies allowed.