int i;
string name, nameSearch, endLine, line, newName, nameOfCustomer;
ofstream readData2 ("informationFinal.dat");
ifstream readData ("information.dat");
stack<int> numStore;
stack<string> nameStore;
if(readData.fail() == true){ // This will check if the file exists or not
cout << "Error: cannot find file!" << endl;
system("PAUSE");
return 1;}
cin >> newName;
readData >> i;
i = i + 1;
numStore.push(i);
getline(readData,line);
for (int j = 0; j < i -1; j++)
{
readData >> nameOfCustomer;
nameStore.push(nameOfCustomer);
getline(readData,line);
}
nameStore.push(newName);
readData2 << numStore.top() << "\n";
for (int k = 0; k < i; k++)
{
readData2 << nameStore.top() << "\n";
nameStore.pop();
}
This reads everything from the file information.dat, then adds a new name and increments the first number by 1 and prints all that to the informationFinal.dat. I don't want it to do that because then I won't be able to update it again.
When I changed ofstream readData2 ("informationFinal.dat"); to ofstream readData2 ("information.dat");, it deleted everything from the file I had and won't be able to read anything. Try it yourself to know what I mean.