visual c++ - C++ why is my file truncating when I restart my program? -


i'm trying use code read file , store values in vector. works once , displays correctly.

void songlist::loadsongsfromfile() {     song temp;     string line;     ifstream myfile("songlistfile.txt");     while (getline(myfile, line)) {         myfile >> temp.title;         myfile >> temp.artist;         myfile >> temp.genre;         songs.push_back(temp);     } } 

i want append file, using following:

void songlist::addsong(song tmp) {     cout << "enter title, artist genre of song, each on new line.\n";     cin >> tmp.title;     cin >> tmp.artist;     cin >> tmp.genre;     songs.push_back(tmp);     ofstream myfile("songlistfile.txt");     myfile.open("songlistfile.txt", ios::app);     myfile << tmp.title << " " << tmp.artist << " " << tmp.genre;     cout << tmp.title << " " << tmp.artist << " part of song library! "; } 

everything works fine, file wipe , have nothing in once finish program, if have tried append file. clear, need previous contents, , new lines have added there every time reopen program.

std::ofstream myfile("songlistfile.txt"); opens file , truncates. should use std::ofstream myfile("songlistfile.txt", std::ios::app);

otherwise can declare myfile object std::ofstream myfile; , open file using , append option: myfile.open("songlistfile.txt", std::ios::app);


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -