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
|
void modifica_contatto::edit_contatto() {
QString parte1,parte2;
//name and surname are two attributes. I need them to find the contact that
//I have to modify
parte1 = this->name;
parte2 = this->surname;
fstream a,b;
a.open("./agenda.txt",ios::in|ios::out);
b.open("./agenda.txt",ios::in|ios::out);
string line1,line2;
bool nome_trovato,cognome_trovato,fine_ciclo;
//QString is a QTcreator type
QString Qline1,Qline2,Qline1a,Qline2a;
QString Qline3,Qline4,Qline5,Qline6,Qline7;
string line1a,line2a,line3,line4,line5,line6,line7;
fine_ciclo=false;
while(!a.eof() && !fine_ciclo){
//I get the first 2 lines of the file
a>>line1;
a>>line2;
//I have to convert QString to std::string to compare parte1 with line1
//and parte2 with line2
Qline1a=Qline1a.fromStdString(line1);
Qline2a=Qline2a.fromStdString(line2);
nome_trovato=true;
cognome_trovato=true;
if(parte1 != Qline1a || Qline1a.isEmpty()) nome_trovato=false;
if(parte2 != Qline2a || Qline2a.isEmpty()) cognome_trovato=false;
//if parte1==Qline1 && parte 2==Qline2, the method starts to change the
//contact information
if(nome_trovato==true && cognome_trovato==true){
//the method takes the values to modify the contact with
Qline1=ui->nameline_m->text();
Qline2=ui->secondNameLine_m->text();
Qline3=ui->via_m->text();
Qline4=ui->number_m->text();
Qline5=ui->interno_m->text();
Qline6=ui->tel_m->text();
Qline7=ui->email_m->text();
//I convert QString to std::String
line1a=Qline1.toStdString();
line2a=Qline2.toStdString();
line3=Qline3.toStdString();
line4=Qline4.toStdString();
line5=Qline5.toStdString();
line6=Qline6.toStdString();
line7=Qline7.toStdString();
//I write the strings (now std::string) in the .txt file
//here the strings are not appended, but I found the contact that
//I have to modify, and I modify the fields of that contact
b<<line1a<<endl;
b<<line2a<<endl;
b<<line3<<endl;
b<<line4<<endl;
b<<line5<<endl;
b<<line6<<endl;
b<<line7<<endl;
//fine_ciclo stops the iteration and make the program exit from the
//cycle
fine_ciclo=true;
}
//if the first 2 lines aren't the same of name and surname, a and b go
//to the next contact inside the file
else{
for(int i=0;i<5;i++) {a>>line1;b>>line1;}
}
line1.clear();
line2.clear();
line3.clear();
line4.clear();
line5.clear();
line6.clear();
line7.clear();
line1a.clear();
line2a.clear();
Qline1.clear();
Qline2.clear();
Qline3.clear();
Qline4.clear();
Qline5.clear();
Qline6.clear();
Qline7.clear();
}
a.close();
b.close();}
|