c++ database delete all the data before "fin"...

This code gets data from a file and stores them in a vector(or actually it has to get data) but it deletes the data in file and gets nothing...

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
//Here is the struct
struct bio{
    string name;
    string job;
    int age;
    string address;
    char gender;
    string hobby;
};

vector<bio> newones;
  void delbio()
{
    ofstream fout;
    ifstream fin;
    string kot;
    char a,b;
    fin.open("1.txt");
    fout.open ("1.txt");
    bio arrays;
//ERROR:Deletes everything before getting the data
    arrays.name="h";
     while (arrays.name.length()>0)
        {
            getline (fin,arrays.name);
            if (arrays.name.length()<=0)
                {
                    break;
                }
            getline (fin,arrays.job);
            fin >> arrays.age;
            fin >> a;
            getline (fin,arrays.address);
            arrays.address=a+arrays.address;
            fin >> arrays.gender;
            fin >> b;
            getline (fin,arrays.hobby);
            arrays.hobby=b+arrays.hobby;
            getline(fin,kot);
            newones.push_back(arrays);


}
//Part that deletes data
            int x=0;
            string str1;
            char s;
            cout << "Enter name and surname (Both with first letter in capital)" << endl;
            cin >> s;
            getline(cin,str1);
            str1= s+str1;

        while (x<newones.size())
        {
            if(newones[x].name==str1)
                {
                    newones.erase(newones.begin()+x);
                }
                    cout << newones[x].name << endl;
            x++;
        }

//Print new vector 
        int w=0;
        while (w<newones.size())
        {
            cout << newones.size() << "\n";
            fout << newones[w].name << "\n";
            cin.get();
            fout << newones[w].job << "\n";
            fout << newones[w].age << "\n";
            fout << newones[w].address<< "\n";
            fout << newones[w].gender << "\n";
            fout << newones[w].hobby << "\n*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*\n";
            w++;
        }
        fout.close();
}
try to read the data first before the

fout.open ("1.txt");
Last edited on
Not at all... It needs to be opened to use that file... Cant use it without opening it...
I think what rafae11 meant was to do all your reading from file before you write to the file.
You open the file for both reading and writing with two separate file streams. So in other words when you are done reading do fin.close(); then do fout.open("1.txt");
OK I will try
It helped... Thanks to both of you
Topic archived. No new replies allowed.