No idea to print last item from the .txt file

In case 2, I need to print the most recently added song in index list file.
I have no idea to do this part, any idea ? Any criticism is welcomed.
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
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std; 
void menu(int *);
void option(fstream &, int *);
int main()
{
    int a, p; 
    do 
    {
    menu(&a);
    fstream myfile; 
    myfile.open("music.txt" , ios::app | ios::out);
    myfile.close();
    p=a;
    option(myfile, &p);
    }while(a==1||2||3||4||5);
    system("pause");
    return 0; 
}
void menu(int *e)
{
     cout<<"\n\nMusic index list \n"
         <<"---------------- \n"
         <<"1. Add new song title to index list file \n"
         <<"2. Print most recently added song title in index list file \n"
         <<"3. Print all the items from the index list file \n"
         <<"4. Delete all of the items on the index list file \n"
         <<"5. Quit \n\n";
     cin>>*e;
     cout<<endl<<endl;
}
void option(fstream &myfile, int *p)
{
     char ch; 
     string line; 
     int pass; 
     
     switch (*p)
     {
            case 1: 
                 {
                 myfile.open("music.txt", ios::app | ios::out);
                 cout<<"Please end the title with a period . \n";
                 cout<<"What is the new title ?\n ";
                 cin.get(ch);
                 while (ch != '.')
                 {
                       myfile.put(ch);
                       cin.get(ch);
                 }
                 myfile.put(ch);
                 myfile.close();
                 break; 
                 }
            case 2:
                 {
                 myfile.open("music.txt", ios::in);    //Need to print the last song title from the file
                 myfile.close();
                 break; 
                 }
            case 3:
                 {
                 myfile.open("music.txt");
                 if (myfile.is_open())
                 {
                 while (myfile.good())
                 {
                 getline (myfile, line);
                 cout<<line<<endl;
                 } 
                 myfile.close();
                 }
                 else cout<<"Unable to open file.\n";
                 break; 
                 }
            case 4:
                 {
                 cout<<"Warning, confirm to delete.\n"
                     <<"Please press 1. \n";
                 cin>>pass; 
                 if(pass==1)
                 {
                 myfile.open("music.txt", ios::out);
                 cout<<"Data deleting......\n";
                 myfile.close();
                 }
                 else
                 cout<<"Try again.\n";
                 break; 
                 }
            case 5:
                 cout<<"warning, confirm program to close.\n"
                     <<"Press 1\n";
                 exit(0);
     }
}
     
                 
         
     

One way to do it is by reading the entire file into a string and do a string.find_last_of(' '). Then you can just extract a substring that contains the last song.

Or you could read each string in the file into a vector and print the last one.
Thank you very much. I'm very appreciate it.
Topic archived. No new replies allowed.