Print last entered date from .txt file

  • Thread starter Thread starter tebes
  • Start date Start date
  • Tags Tags
    File
Click For Summary

Discussion Overview

The discussion revolves around retrieving the last entered data from a .txt file in a C++ program, specifically focusing on file handling and menu-driven user input. Participants explore various coding practices and logic errors in the provided code snippet.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants suggest using a while loop with ifstream::getline() to read the last line from the file until reaching the end of the file.
  • Concerns are raised about variable naming conventions, with suggestions to use more descriptive names instead of single letters.
  • Participants point out a logical error in the while loop condition that checks the variable 'a', indicating it will execute for any positive integer.
  • There are questions regarding the multiple calls to myfile.open(), with suggestions that it may be unnecessary to open the file multiple times for different operations.
  • Some participants express confusion about the functionality of the fourth menu option, which is intended to delete file contents but only opens and closes the file without actual deletion.
  • One participant defends the use of multiple open calls by stating they are necessary for different access flags for specific functions.
  • Another participant mistakenly believes that opening a file with ios::out will erase its contents upon opening.

Areas of Agreement / Disagreement

Participants generally agree on the need for clearer variable names and the potential inefficiency of multiple file openings. However, there is disagreement regarding the implementation of the file deletion functionality and the understanding of file access flags, indicating that the discussion remains unresolved.

Contextual Notes

Limitations include unclear assumptions about file handling behavior and the incomplete implementation of the deletion functionality in the provided code.

Who May Find This Useful

Readers interested in C++ programming, file handling, and coding best practices may find this discussion beneficial.

tebes
Messages
38
Reaction score
0

Homework Statement


How do I retreat the last entered data from .txt file ?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std; 
void menu(int *);
void option(fstream &, int *);
int main()
{
    int a, p; 
    while(a== 1||2||3||4||5 ) 
    {
    menu(&a);
    fstream myfile; 
    myfile.open("music.txt" , ios::app | ios::out);
    myfile.close();
    p=a;
    option(myfile, &p);
    }
    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::out);    //Need to print the last song title from the file
                 
                 myfile.close();
                 break; 
                 }
            case 3:
                 {
                 cout<<"My music: \n";
                 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, program to be closed.\n";
                  exit(0);
                  }
     }
}
 
Last edited by a moderator:
Physics news on Phys.org
Are you asking how to retrieve the last-entered song title? If so, you could write a while loop that reads and saves in a variable a line of data at a time from the file (using ifstream::getline()). The loop should run until ios::eof() returns true. When ios::eof() returns true, you have reached the end of the file, and the variable holds the last string that was read.

Some comments on your code:
1) a, p, and e are really dumb names for variables. It's impossible to tell what they are supposed to represent. Use longer names that are more explanatory.
2) This code doesn't do what you think it's doing.
Code:
while(a== 1||2||3||4||5 ) 
{
   ...
}
The while loop will continue executing if a is any positive integer. You need to check the boolean expressions separately: (a == 1) || (a == 2) || etc.

3. Why are you opening the music.txt file so many times? I count five calls to myfile.open( ... ).

4. Your fourth menu item (delete the file) doesn't do anything useful. Maybe you just don't have that fully implemented yet.
 
Mark44 said:
Are you asking how to retrieve the last-entered song title? If so, you could write a while loop that reads and saves in a variable a line of data at a time from the file (using ifstream::getline()). The loop should run until ios::eof() returns true. When ios::eof() returns true, you have reached the end of the file, and the variable holds the last string that was read.

Some comments on your code:
1) a, p, and e are really dumb names for variables. It's impossible to tell what they are supposed to represent. Use longer names that are more explanatory.


2) This code doesn't do what you think it's doing.
Code:
while(a== 1||2||3||4||5 ) 
{
   ...
}
The while loop will continue executing if a is any positive integer. You need to check the boolean expressions separately: (a == 1) || (a == 2) || etc.


3. Why are you opening the music.txt file so many times? I count five calls to myfile.open( ... ).


4. Your fourth menu item (delete the file) doesn't do anything useful. Maybe you just don't have that fully implemented yet.

Thank you for you suggestion. Those tips really helpful. I'm still a beginner.


Reply 1 : I agree with you. Those are really dumb variables. I apologize for giving trouble to you or whoever who read this codes.

Reply 2 : You are right. Thank you so much.

Reply 3 : The reason I open multiple of myfile.open(), is to put different access flag , to carry out their specific function.

Reply 4 : The fourth menu option is to clear the entered items on .txt .
 
tebes said:
Reply 3 : The reason I open multiple of myfile.open(), is to put different access flag , to carry out their specific function.
I see that, but you still might be opening the file more than you need to. I don't think that you need to open the file in main() at all, since you are opening it several times in your option function.
tebes said:
Reply 4 : The fourth menu option is to clear the entered items on .txt .
But it says it is deleting the items in the file, but all it really does is close the file.
Code:
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;
}
 
Mark44 said:
I see that, but you still might be opening the file more than you need to. I don't think that you need to open the file in main() at all, since you are opening it several times in your option function.

But it says it is deleting the items in the file, but all it really does is close the file.
Code:
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;
}

"I see that, but you still might be opening the file more than you need to. I don't think that you need to open the file in main() at all, since you are opening it several times in your option function."

Reply: I was requested to do that , by my professor. ^.^ I am sorry for ambiguousness . Thank you, by the way.

But it says it is deleting the items in the file, but all it really does is close the file.
Code:
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;
}

Reply: I thought access flag " ios::out", was supposed to erase everything on the .txt when the program runs
 
tebes said:
Reply: I thought access flag " ios::out", was supposed to erase everything on the .txt when the program runs
OK, I think you're right. If you open a file for output, that effectively wipes out the previous contents of the file.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K