حذف یک رکورد از فایل در c++
نویسنده : رضا قربانی | زمان انتشار : 17 دی 1400 ساعت 21:46
جهت انجام پروژه های دانشجویی و یا تمرینهای برنامه نویسی رشته کامپیوتر میتوانید به آی دی تلگرام زیر پیام دهید
@AlirezaSepand
سلام دوستان عزیز من یه برنامه به زبان ++c دارم میخواستم قسمت حذف رو بهش اضافه کنم کسی میتونه کمک کنه کدش رو برام اضافه کنه خیلی ضروری است برای فردا میخوامش
#include "iostream.h"
#include "conio.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "fcntl.h"
#define max 50
class student {
public :
char numst[11];
char name[15];
char famili[20];
double score[4];
double ave;
student(); //initializing
~student(); //destructing
set_null(){name[0]=numst[0]='\0';}
}st[max];
//************************************
student::student(){ //initializing
set_null();
}
//*************************************
student::~student(){ //destructing
set_null();
}
//*************************************
class Enter {
private :
void save();
public :
void add();
void edit();
void search();
void print();
void len_file();
void load();
}e;
//************************************************** ****************************
void Enter::add(){
int i;
double sum=0.00;
for(i=0;i<max && st[i].name[0]!='\0';i++);
clrscr();
cout<<"\n Enter a name : ";
cin>>st[i].name;
cout<<"\n Enter a famili : ";
cin>>st[i].famili;
cout<<"\n Enter a numst : ";
cin>>st[i].numst;
for(int j=0;j<4;j++){
cout<<"\n Enter a score "<<j+1<<" : ";
cin>>st[i].score[j];
sum+=st[i].score[j];
}
st[i].ave=sum/4;
e.save();
}
//************************************************** ****************************
void Enter::edit(){
char tmp[11];
int i,flag=0;;
double sum=0.00;
clrscr();
cout<<"\n Enter a numst for edit : ";
cin>>tmp;
for(i=0;i<max && st[i].name[0]!='\0';i++)
if(strcmp(st[i].numst,tmp)==0){
cout<<"\n Enter a new name : ";
cin>>st[i].name;
cout<<"\n Enter a new famili : ";
cin>>st[i].famili;
for(int j=0;j<4;j++){
cout<<"\n Enter a new score "<<j+1<<" : ";
cin>>st[i].score[j];
sum+=st[i].score[j];
}
st[i].ave=sum/4;
cout<<"\n for continue . press key...";
getch();
flag=1;
e.save();
}
if(flag==0){
cout<<"\n Not found . press key...";
getch();
return ;
}
}
//************************************************** ****************************
void Enter::search(){
char tmp[15];
int i,flag=0;
clrscr();
cout<<"\n Enter a name for search : ";
cin>>tmp;
for(i=0;i<max && st[i].name[0]!='\0';i++)
if(strcmp(st[i].name,tmp)==0){
cout<<"\n Name = "<<st[i].name;
cout<<"\n Famili = "<<st[i].famili;
for(int j=0;j<4;j++)
cout<<"\n Score "<<j+1<<" = "<<st[i].score[j];
cout<<"\n average = "<<st[i].ave;
cout<<"\n for continue . press key...";
getch();
flag=1;
}
if(flag==0){
cout<<"\n Not found . press key...";
getch();
return ;
}
}
//************************************************** ****************************
void Enter::print(){
int i;
clrscr();
for(i=0;i<max && st[i].name[0]!='\0';i++){
cout<<"\n Name = "<<st[i].name;
cout<<"\n Famili = "<<st[i].famili;
for(int j=0;j<4;j++)
cout<<"\n Score "<<j+1<<" = "<<st[i].score[j];
cout<<"\n average = "<<st[i].ave<<"\n\n";
}
cout<<"\n for continue . press key...";
getch();
}
//************************************************** ****************************
void Enter::save(void){
register int i;
FILE *fp;
fp=fopen("c:\\st.dat","w+b");
if(!fp){
cout<<"\n Cannot open file press key ...";
getch();
return ;
}
for(i=0;i<max && st[i].name[0]!='\0';i++)
fwrite(&st[i],sizeof(class student),1,fp);
fclose(fp);
}
//************************************************** ****************************
void Enter::load(void){
register int i;
FILE *fp;
clrscr();
fp=fopen("c:\\st.dat","rb");
if(!fp){
cout<<"\n Cannot open file press key ...";
getch();
return ;
}
for(i=0;i<max;i++){
fread(&st[i],sizeof(class student),1,fp);
if(feof(fp)){
clrscr();
cout<<" Data loaded press a key ...";
getch();
return ;
}
}
fclose(fp);
}
//************************************************** ****************************
void Enter::len_file(){
int handle;
clrscr();
handle=open("c:\\st.dat",O_RDONLY);
cout<<"\n File lenght in byte = "<<filelength(handle)<<" byte ";
getch();
close(handle);
}
//************************************************** ****************
void main(){
int num;
e.load();
do{
clrscr();
cout<<"\t\t Welcome\n";
cout<<"\n 1).Add";
cout<<"\n 2).Edit";
cout<<"\n 3).Search";
cout<<"\n 4).List";
cout<<"\n 5).Length file";
cout<<"\n 6).Exit";
cout<<"\n\n Enter a number (1-6) :";
num=getche()-48;
getch();
switch (num){
case 1 : e.add();
break;
case 2 : e.edit();
break;
case 3 : e.search();
break;
case 4 : e.print();
break;
case 5 : e.len_file();
break;
case 6 : exit(1);
}
}while(1);
}// end program
منبع: barnamenevis.org