Thread: File Output

  1. #31
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Are you typing the correct path name or is it in your current folder?

    I tested it and it worked fine.
    Sent from my iPadŽ

  2. #32
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Here is the entire code again showing withe the delete section:
    Code:
    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string name = "";
      string realfirst = "";
      string lastname = "";
      string datejoined = "";
      string email = "";
      string search = "";
      string type = "";
      string finish = "";
      string file = "";
      string deletemem = "";
    
      do
      {
         cout<<"Welcome To The Tyranno Database"<<endl;
         cout<<endl;
         cout<<"What would you like to do? (press i for instructions): ";
         getline(cin, type);
         cout<<endl;
         system ("CLS");
    
         if (type == "i"){
            cout<<endl;
            cout<<"This is Tyranno Database Software v0.1"<<endl;
            cout<<endl;
            cout<<"This software can save information about Tyranno members"<<endl;
            cout<<endl;
            cout<<"To do this, type create into the opening screen"<<endl;
            cout<<endl;
            cout<<"To search for a member, type search into the opening screen"<<endl;
            cout<<endl;
            cout<<"To edit member information just use the create function"<<endl;
            cout<<endl;
            cout<<"To delete a member, type delete into the opening screen"<<endl;
            cout<<endl;
            cout<<"Press Enter";
            cin.get();
            system ("CLS");
            finish = "no";
         }
    
         else if (type == "create"){
            cout<<endl;
            cout<<"Please enter the username of the member: ";
            getline(cin, name);
            cout<<endl;
            name += ".txt";
    	    ofstream a_file ( name.c_str() );
            a_file<<name<<std::endl;
            cout<<"Please enter the member's first name: ";
            getline(cin, realfirst);
            cout<<endl;
            a_file<<realfirst<<std::endl;
            cout<<"Please enter the member's last name: ";
            getline(cin, lastname);
            cout<<endl;
            a_file<<lastname<<std::endl;
            cout<<"Please enter the date the member joined: ";
            getline(cin, datejoined);
            cout<<endl;
            a_file<<datejoined<<std::endl;
            cout<<"Please enter the member's e-mail address: ";
            getline(cin, email);
            cout<<endl;
            a_file<<email<<std::endl;
            a_file.close();
            system ("CLS");
            cout<<"Would you like to finish? (yes / no): ";
            getline(cin, finish);
            system ("CLS");
         }
    
         else if (type == "search") {
            cout<<"Please enter the username of the member: ";
            getline(cin, search);
            cout<<endl;
            file = search + ".txt";
            ifstream b_file ( file.c_str() );
               if (!b_file.is_open()) {
                  cout<<"That file could not be opened"<<endl;
                  cout<<endl;
                  cout<<"Would you like to finish? (yes / no): ";
                  getline(cin, finish);
                  system ("CLS");
               }
               else {
                  cout<<"Username: "<<search<<endl;
                  cout<<endl;
                  b_file>>name;
                  b_file>>realfirst;
                  b_file>>lastname;
                  cout<<"Full name: "<<realfirst + " " + lastname<<endl;
                  cout<<endl;
                  b_file>>datejoined;
                  cout<<"Joined Tyranno on: "<<datejoined<<endl;
                  cout<<endl;
                  b_file>>email;
                  cout<<"E-mail address: "<<email<<endl;
                  cout<<endl;
                  cout<<"Press Enter";
                  cin.get();
                  b_file.close();
                  cout<<endl;
                  system ("CLS");
                  cout<<"Would you like to finish? (yes / no): ";
                  getline(cin, finish);
                  system ("CLS");
               }
         }
    
         else if (type == "delete") {
            cout<<"Please enter the username of the member to delete: ";
            getline(cin,name);
            cout<<endl;
            cout<<"Are you sure that you would like to delete the member "<<name<<"? ";
            getline(cin, deletemem);
            cout<<endl;
               if (deletemem == "yes") {
                  name += ".txt";
                  ofstream c_file ( name.c_str() );
                  if (!c_file.is_open()) {
                      system ("CLS");
                      cout<<"That file does not exist to be deleted"<<endl;
                      cout<<endl;
                      cout<<"Press Enter";
                      cin.get();
                      finish == "no";
                  }
                  else {
                     remove ( name.c_str() );
                     cout<<"Deleted"<<endl;
                     cout<<endl;
                     cout<<"Press Enter"<<endl;
                     cin.get();
                     finish == "no";
                     system ("CLS");
                  }
               }
    
               else {
                  finish == "no";
                  system ("CLS");
               }
         }
    
         else {
            cout<<"Sorry, Tyranno Database Software does not want to do that!"<<endl;
            cout<<endl;
            cout<<"Would you like to finish? (yes / no): ";
            getline(cin, finish);
            system ("CLS");
         }
       }
       while (finish == "no");
    }
    When you search for the deleted file, it still manages to open it.

  3. #33
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
           else {
                     c_file.close();   // VERY VERY VERY IMPORTANT!
                     remove ( name.c_str() );
                     cout<<"Deleted"<<endl;
                     cout<<endl;
                     cout<<"Press Enter"<<endl;
                     cin.get();
                     finish == "no";
                     system ("CLS");
                  }
    Sent from my iPadŽ

  4. #34
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yehaa

  5. #35
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It works fine now, i had a problem in that it would close after deletion but i just took out the cin.get() and put it back in, compiled and it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM