File Handling and Structs

Here I have problem with the delete function
i have to delete a student's record whose roll no is given but my delete function
does not work fine ........
and also my teacher told me that you are doing the wrong way ..you should not use array of objects or any other array
please tell me if there is any other way to solve it plzzzzzzz...
Here is my code..
using namespace std ;


1
2
3
4
5
6
7
8
9
10
11
struct student
{
	
	
	char name[30] ;
	char gender ;
	int roll_no ;
	int p_marks, c_marks, m_marks, e_marks, cs_marks ;
	float per ;//percentage
	char grade ;
}st[10];


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
int main()
{
	
	int select ;
	do
	{
	
	cout << "Enter Choice (1-3) \n" ;
	cin>>select ;
	system("cls") ;
		switch(select)
		{	
		case 1:
			class_result();
			cout << "\t\t   Press Any Key To Continue\n\n" ;
			getch() ;
			system("cls") ;
			break ;
			
			case 2:
				entry_menu();
				break ;
				case 3:
					return 0 ;
					break ;
				default:
					cout << " \t\t\tInvalid Input\n" ;
					main();
					break;
				}
	}while( select!=3 );

}



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
void entry_menu()
{
	char choice ;
	
	cout << "Please Enter Choice \n\n" ;
	cin >> choice ;
	system("cls") ;
	
			if ( choice=='c'||choice=='C' )
			{
			write_student();
			cout << "\t\t   Press Any Key To Continue\n\n" ;
			getch() ;
			system("cls") ;
			entry_menu();
		}
			else if ( choice=='s'||choice=='S' )
			{
				display_all();
				cout << "\t\t\t Enter Any Key To Continue \n\n" ;
				getch() ;
				system("cls") ;
				entry_menu();
			}
			else if ( choice=='m'||choice=='M' )
			{
						int rollno ;
						cout << "Enter Student's Roll No to Modify record :" ;
						cin >> rollno ;
					modify_student(rollno);
					cout << "\t\t   Press Any Key To Continue\n\n" ;
					getch() ;
					system("cls") ;
					entry_menu();
				}
				
			else if ( choice=='d'||choice=='D' )
			{
							int del ;
						cout << "Enter Student's Roll No to Delete Record :" ;
						cin >> del ;
						delete_student(del) ;
						cout << "\t\t   Press Any Key To Continue\n\n" ;
						getch() ;
						system("cls") ;
						entry_menu();
					}
				
				else if ( choice=='b'||choice=='B' )
				{
					main();
							}
							else
							{
								cout << "\t\t\tInvalid Input \n\n" ;
								entry_menu();
							}
		entry_menu() ;
	}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void getdata()
{
	
	int i = 0 ;
	cout << "Enter student Name :\n" ;
	cin >> st[i].name ;
	cout << "Enter student Roll No :\n" ;
	cin >> st[i].roll_no ; ;
	cout << "Enter student gender :\n" ;
	cin >> st[i].gender ;
	cout << "Enter student marks in Physics :\n" ;
	cin >> st[i].p_marks ;
	cout << "Enter Chemistry Marks :\n" ;
	cin >> st[i].c_marks ;
	cout << "Enter Maths' Marks :\n" ;
	cin >> st[i].m_marks ;
	cout << "Enter Marks in English :\n" ;
	cin >> st[i].e_marks ;
	cout << "Enter Marks in Computer Science :\n" ;
	cin >> st[i].cs_marks ;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void write_student()
{
	
	getdata() ;
	//cout << "Enter a Key \n\n" ;
	calculate();
	int i = 0 ;
	ofstream infile("C:/Users/MR_Salman/Desktop/expert.txt", ios::app | ios::ate) ;
    infile << st[i].roll_no << " " << st[i].name << " " << st[i].p_marks << " " << st[i].c_marks
	<<" " << st[i].m_marks << " " << st[i].e_marks << " "
	<< st[i].cs_marks << " " << st[i].per << " " << st[i].grade << " \n" ;
	infile.close() ;
	cout << "\t\t\tData is Written \n\n\n";
	
}


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 display_all()//reading data 
{
    ifstream infile("C:/Users/MR_Salman/Desktop/expert.txt",ios::in|ios::binary) ;
    int i = 0 ;
    initialize_struct() ;
 	for ( i =0 ; i <= 9 ; i++ )
	{
		
	
		infile >> st[i].roll_no ;
		infile >> st[i].name ;
		infile >> st[i].p_marks ;
		infile >> st[i].c_marks ;
		infile >> st[i].m_marks ;
		infile >> st[i].e_marks ;
		infile >> st[i].cs_marks ;
		infile >> st[i].per ;
		infile >> st[i].grade ;
		
	}
	i=0 ;
infile.close() ;
showData() ;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void showData()
{

cout << "Roll No  Name   P  C  M  E  CS  %AGE  GRADE \n" ;	
int i = 0 ;

for ( i =0 ; i <= 9 ; i++ )
	{
		
		if ( st[i].roll_no!=0 )
		{
		cout << st[i].roll_no <<"\t" << st[i].name<< " " << st[i].p_marks << " " << st[i].c_marks << " " << st[i].m_marks << " "
		<< st[i].e_marks << " " << st[i].cs_marks << " " << st[i].per << "        " << st[i].grade << "\n" ;
	}
	else if ( st[i].roll_no==0 )
	{
		break ;
	}
	}
	i = 0 ;
}



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
void modify_student(int a)
{
		int i = 0 ;
        ifstream infile("C:/Users/MR_Salman/Desktop/expert.txt",ios::in|ios::binary) ;
	
	for ( i =0 ; i <= 9 ; i++ )
	{
		
		infile >> st[i].roll_no ;
		infile >> st[i].name ;
		infile >> st[i].p_marks ;
		infile >> st[i].c_marks ;
		infile >> st[i].m_marks ;
		infile >> st[i].e_marks ;
		infile >> st[i].cs_marks ;
		infile >> st[i].per ;
		infile >> st[i].grade ;
		
	}
	i = 0 ;
	for ( i=0 ; i<=9 ;i++ )
	{
		
		
		if ( st[i].roll_no==a )
		{
			
			cout << "Enter student Name :\n" ;
			cin >> st[i].name ;
			cout << "Enter student Roll No :\n" ;
			cin >> st[i].roll_no ; ;
			cout << "Enter student gender :\n" ;
			cin >> st[i].gender ;
			cout << "Enter student marks in Physics :\n" ;
			cin >> st[i].p_marks ;
			cout << "Enter Chemistry Marks :\n" ;
			cin >> st[i].c_marks ;
			cout << "Enter Maths' Marks :\n" ;
			cin >> st[i].m_marks ;
			cout << "Enter Marks in English :\n" ;
			cin >> st[i].e_marks ;
			cout << "Enter Marks in Computer Science :\n" ;
			cin >> st[i].cs_marks ;
			calculate() ;
			
			ofstream out("C:/Users/MR_Salman/Desktop/expert.txt");
			for ( i = 0 ; i < 9 ; i++ )
			{
				if ( st[i].roll_no !=0 )
		
			{
				out << st[i].roll_no << " " << st[i].name << " " << st[i].p_marks << " " << st[i].c_marks
				<<" " << st[i].m_marks << " " << st[i].e_marks << " "
				<< st[i].cs_marks << " " << st[i].per << " " << st[i].grade << " \n" ;
	
			}
			
			}
			cout << "\t\t\tData is Modified \n\n" ;
			out.close();
			break ;
		}
		else if ( i==9 && st[i].roll_no!=a )
		{
			cout << "Record Not Found \n\n" ;
			break ;
		}
	
	}
	infile.close();

}

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
//-------------------------------------------------------DELETING STUDENT RECORD-------------------------------------------------------------------------------

void delete_student(int a)
{
		int i = 0 ;
		
	//	cout << "\t\tARE YOU SURE YOU WANT TO DELETE ?Y/N\n\n" ;
	//	char ch ;
	//	cin >> ch ;
        ifstream infile("C:/Users/MR_Salman/Desktop/expert.txt",ios::in|ios::binary) ;
		
	for ( i =0 ; i <= 9 ; i++ )
	{
		
		
		infile >> st[i].roll_no ;
		infile >> st[i].name ;
		infile >> st[i].p_marks ;
		infile >> st[i].c_marks ;
		infile >> st[i].m_marks ;
		infile >> st[i].e_marks ;
		infile >> st[i].cs_marks ;
		infile >> st[i].per ;
		infile >> st[i].grade ;
		
	} 
	i = 0 ;
	infile.close();
	
	ofstream out("C:/Users/MR_Salman/Desktop/expert.txt",ios::out);
	for ( i =0 ; i <=9 ; i++ )
	{
		if ( st[i].roll_no==a )
		{
			
		}
		else if ( st[i].roll_no!=0 )
		{
			out << st[i].roll_no << " " << st[i].name << " " << st[i].p_marks << " " << st[i].c_marks
			<<" " << st[i].m_marks << " " << st[i].e_marks << " "
			<< st[i].cs_marks << " " << st[i].per << " " << st[i].grade << " \n" ;
		}
		
	}
	/*if ( ch=='y'|| ch=='Y' )
	{
	ofstream out("C:/Users/MR_Salman/Desktop/expert.txt");
	for ( i = 0 ; i <= 9 ; i++ )
	{
			 if ( st[i].roll_no!=a  )
			{
				if ( st[i].roll_no!=0 )
				{
				out << st[i].roll_no << " " << st[i].name << " " << st[i].p_marks << " " << st[i].c_marks
				<<" " << st[i].m_marks << " " << st[i].e_marks << " "
				<< st[i].cs_marks << " " << st[i].per << " " << st[i].grade << " \n" ;
				}
			}
	
	}
	i = 0 ;
	cout << "Record is Deleted \n\n" ;
	out.close();
}*/
}


Also My teacher atold me Not to use array of struct object st[10] .He asked me to use only one object st .
Last edited on
Please use code tags - it helps everyone as without them the code is very difficult to read. http://www.cplusplus.com/articles/jEywvCM9/

If you want to delete a line from a file, a usual way is to read the file one line at a time, and write it out to a new file one line at a time. When you reach the line which is to be deleted, simply don't write that out to the file, but instead continue with the next line.
Can You tell me plz how to use seekp() ,seekg() and tellp() etc to get positions
I don't see how those functions would help with the original goal,
i have to delete a student's record


You can find help on most standard functions by using the search box at the top of the page,
http://www.cplusplus.com/reference/ostream/ostream/seekp/
http://www.cplusplus.com/reference/istream/istream/seekg/
http://www.cplusplus.com/reference/ostream/ostream/tellp/
Thank you bro
if you please tell me that how to read a particular roll no and modify it ???????

i have the following text file

i created the code but i can't get it working fine

and also i can't use code tags becoz i dont know to use ....so plz dont mind if i dont use code tags ....
Here is my file and i want to modify and then delete a given roll no



354 Azeem 87 86 85 84 83 85 C
81 Usama 98 90 81 83 88 88 C
57 hassan 87 81 90 82 83 84.6 D
9 Akash 78 90 98 81 83 86.4 C
45 Sibgo 78 87 97 79 90 85.4 C
24 Salman 76 78 71 81 98 80.8 D
360 Ans 77 78 71 90 94 82 D
and also i can't use code tags becoz i dont know to use ....so plz dont mind if i dont use code tags ....


Click to edit your post
Highlight your code
Click on the <> button on the right side of the post under Format:

It's that simple :) And it'll make it a lot easier for people to want to try to help.
thanks a lot
hey if any body can help me writing a delete function to delete student record
plz ... i tried a lot but i could not find a way .i wrote a code that works Fine but
my teacher told me not to use array

i wrote code above ...........
plz help me through
If you do as people have suggested, and edit your post to make it readable (using code tags), then we're much more likely to take the time to read it and help.
I have Edited my code now if you plz help me
Looking at your sample data,
354 Azeem 87 86 85 84 83 85 C
81 Usama 98 90 81 83 88 88 C
57 hassan 87 81 90 82 83 84.6 D
9 Akash 78 90 98 81 83 86.4 C
45 Sibgo 78 87 97 79 90 85.4 C
24 Salman 76 78 71 81 98 80.8 D
360 Ans 77 78 71 90 94 82 D 
it would be unreliable to attempt to update this data in situ, directly within the original file. That's because both individual items of data, and each line as a whole, are of variable length. If you try to alter anything the old and new values may be of differing lengths, and thus in making any alteration some other part of the data could be corrupted.

If each line has the same fixed-width format, for example like this:
354  Azeem            87  86  85  84  83  85   C
81   Usama            98  90  81  83  88  88   C
57   hassan           87  81  90  82  83  84.6 D
9    Akash            78  90  98  81  83  86.4 C
45   Sibgo            78  87  97  79  90  85.4 C
24   Salman           76  78  71  81  98  80.8 D
360  Ans              77  78  71  90  94  82   D 
then it could be possible to modify the data in-place.

However, trying to delete a record would still present a problem. That's because the location within the file where that record used to reside will still be there, it cannot just be made to disappear.

The more general solution I already described, read in the data (one line at a time), and write out the changed version to a separate file.
Last edited on
Thank you Very Much
Bro I'm Gonna Try that and hope it will work
Topic archived. No new replies allowed.