PDA

View Full Version : ادیت کردن فایل



white and black
دوشنبه 24 اسفند 1394, 01:29 صبح
با سلام
لطفا سورس منو ببینید و بگید مشکل کد ادیت چی هست که هر کدی میزنی مینویسه پیدا نشد
در ضمن اگر مثلا کد 3 رو میزنی برا جستجو یا هرچیز دیگه کد بعدی که 4 باشه رو برات میاره
ممنون میشم جوابمو بدید .
#include "stdafx.h"
#include<iostream>
#include<fstream>

using std::fstream;
using namespace std;


struct student{
int code;
char name[10];
}st;

fstream p;

void getstudent();
void writefile();
int backcode(int id);
void print();
void edit();

int _tmain(int argc, _TCHAR* argv[])
{
char ch;
while(1){
cout<<"\n\t\t1:Input 2:Show 4:Edit 3:Exit\n";
cout<<"\n\t\tYour Choice:";
cin>>ch;
system("cls");
switch(ch){
case'1':writefile();
break;
case'2':print();
break;
case'3':exit(0);
break;
case'4':edit();
break;
}
}
return 0;
}

int backcode(int id){
p.open("h:\\Clients.txt",ios::in|ios::binary);
p.seekg(sizeof(struct student)*id,ios::beg);
p.read((char*)&st,sizeof(struct student));
p.close();
if (id==st.code){
return id;
}
return -1;
}

void getstudent(){
int e;
cout<<"Code:";
cin>>e;
while(backcode(e)!=-1){
cout<<"Code Exist ";
cin>>e;
}
st.code=e;
cin.ignore();
cout<<"Name:";
cin.getline(st.name,10);
}

void writefile(){
getstudent();
p.open("h:\\Clients.txt",ios::out|ios::app);
p.seekp(sizeof(struct student)*st.code,ios::beg);
p.write((char*)&st,sizeof(struct student));
p.close();
}

void print(){
student temp;
p.open("h:\\Clients.txt",ios::in);
p.read((char*)&temp,sizeof(struct student));
while(!p.eof()){
if (temp.code!=0){
cout<<temp.code<<" : "<<temp.name<<endl;
}
p.read((char*)&temp,sizeof(struct student));
}
p.close();
}

void edit(){
int q,w;
cout<<"Enter a Code For Edit:";
cin>>q;
w=backcode(q);
if (w==-1){
cout<<"Not Find!!!";

}else{
p.open("h:\\Clients.txt",ios::in|ios::app);
p.seekg(sizeof(struct student)*q,ios::beg);
p.read(reinterpret_cast<char*>(&st),sizeof(struct student));
p.close();

cout<<st.code<<":"<<st.name<<endl;
cout<<"New Name:";
cin.ignore();
cin.getline(st.name,10);

p.open("h:\\Clients.txt",ios::out|ios::app);
p.seekp(sizeof(struct student)*q,ios::beg);
p.write(reinterpret_cast<char*>(&st),sizeof(struct student));
p.close();
}
}