// Mr. Minich // CMPSC 101 // Ch. 5 Demo Program #9 // Nov. 14, 2002 // Purpose - inserts a song into a specified position of a sequential access text file #include <iostream> #include <fstream> #include <string> using namespace std;
int main() { int position = 0; // position of new song in sequential access file string newSong; // user's new song input string nextSong; // next song read from file ifstream infile("songs.txt"); // file of songs ofstream tempfile("temp.txt"); // temp file for song transfers int i; cout << "Enter a new song: "; cin >> newSong; cout << "What is the position of this new song in your favorites list? "; cin >> position; for (i = 0; i < position - 1; i++) { infile >> nextSong; tempfile << nextSong << endl; } tempfile << newSong << endl; while (!infile.eof()) { infile >> nextSong; tempfile << nextSong << endl; } infile.close(); tempfile.close(); ofstream outfile("songs.txt"); ifstream infile2("temp.txt"); while (!infile2.eof()) { infile2 >> nextSong; outfile << nextSong << endl; } infile2.close(); outfile.close(); return 0; }// end of main