.txt file output issues

Hi guys! I would like to say sorry for my english!

I'm developing a software with qt creator, and I have a problem with a method:

This method modifies a .txt files that already exists: I want to replace some rows inside this file.

Andrea
Frappone
Recchia
11
1
06/593726666
frappone@hotmail.it

Mario
Sorrento
Deigladioli
13
1
06/6666666
mario.sorrento@hotmail.it


So the 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
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();}


If I try to modify the 4th field of the first contact (String "Andrea"), the function gives to me this output

Andrea
Frappone
Recchia
11
1
MODIFIED
frappone@hotmail.it
ail.it //this is the issue... why the method adds this string?

Mario
Sorrento
Deigladioli
13
1
06/6666666
mario.sorrento@hotmail.it


I hope you can help me.

Thanks in advance
Last edited on
closed account (S6k9GNh0)
Dear god, help us all.

1) You need to start using vectors or arrays. This is ridiculous and heavily reader unfriendly. If you don't want to do that, or your going to use a predefined struct or similar, please name your variables so they can be easily understood.
2) This may be irrelevant, but files like these can easily be simplified via XML or another similar configuration parsing method. Please try and look into this as it will simplify your life.
3) You need to use only one type of string in your program to simplify things. Well, you don't "need' to, but your most likely going to find someone hand coming across the backside of your head if you don't. Qt should have a streaming class somewhere in its bigass library. Use it instead.
4) The best method for output and input, is to take in a buffer of data and then save that buffer of data. Your using a rather unconventional method to output streams.
Last edited on
ok...but..... the answer to my question?
Was in part 4 of computerquip's explanation.
The file buffer library reference can be found here: http://cplusplus.com/reference/iostream/filebuf/
Also you might find some help here: http://bytes.com/topic/c/answers/63987-replacing-line-file
And here: http://www.daniweb.com/forums/post773315.html#post773315
Topic archived. No new replies allowed.