Thread: File Output

  1. #16
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    #include <iostream>
    #include <iomanip>
    #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 = "";
    
      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<<"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;
            cout<<"Please enter the member's first name: ";
            getline(cin, realfirst);
            cout<<endl;
            a_file<<realfirst;
            cout<<"Please enter the member's last name: ";
            getline(cin, lastname);
            cout<<endl;
            a_file<<lastname;
            cout<<"Please enter the date the member joined: ";
            getline(cin, datejoined);
            cout<<endl;
            a_file<<datejoined;
            cout<<"Please enter the member's e-mail address: ";
            getline(cin, email);
            cout<<endl;
            a_file<<email;
            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>>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 {
            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");
    }
    The problem is in the search section...it opens the file that you search for bu tthen shows all the details in one line but i want to split it up. Compile it, create a file and then search for it and you will see what I ean.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    string filename = "something";
    cout<<filename.c_str()<<endl;
    I don't want my string to be defined, i want it to be user defined
    Then replace the first line with the code that gets the filename from the user and stores it in a <string> variable called "filename".

    And I bet you don't want to display the filename converted to its char array equivalent either. So, use filename.c_str() to create your ifstream object instead.

    Those two lines were an example of how to convert a variable of type <string> to a variable of type char*, which is what the ifstream constructor requires. All functions are defined with certain types as their parameters, and when you call a function, you have to send those types to the function or you will get an error. To create an ifstream object, i.e. the "thing" you need to read from a file, you have to send the function a char* type or a const char*. So, you have two choices:

    1) You can read input from the user into a variable of type char* or const char*. A character array is of type const char* because the name of an array is a pointer to the first element of the array.

    2) You can read input from the user into a variable of type string, and then covert it to type char*. That is easily done by using the string function c_str(), which returns the string as type const char*.
    Last edited by 7stud; 11-13-2005 at 09:13 PM.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Being a noob I dont understand text written answers, I need code and if you could show me where the problem is.

  4. #19
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I am ........ at this but it seems to me like what you are telling me is not what I am wanting to know. Plus I don't know what you are telling me. I know I am stupid at this but the problem is that you are kinda explaining it like you think I am good at all of this.

  5. #20
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Quote Originally Posted by bumfluff
    Code:
            cout<<"Please enter the member's first name: ";
            getline(cin, realfirst);
            cout<<endl;
            a_file<<realfirst;
            cout<<"Please enter the member's last name: ";
            getline(cin, lastname);

    The problem is in the search section...it opens the file that you search for bu tthen shows all the details in one line but i want to split it up. Compile it, create a file and then search for it and you will see what I ean.
    I think your problem is that you are just writing everything to the file without inserting spaces or anything. thats why everything you insert gets attached to eachother.

    if you would change the insert-lines to
    Code:
    a_file << realfirst << " ";
    or a newline in stead of a space, I think your problem is solved.

  6. #21
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    No that does not solve it. When I am back on I will pritn the scfreen that I see.

  7. #22
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    http://img392.imageshack.us/img392/5...een24kg.th.png

    Hopefully you can all see the error that I am having. if someone could show me what is wrong I would be very grateful!

  8. #23
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    here is the changed code, I compiled and tested it and it works perfectly (well, solves your problem at least) for me.

    Code:
    #include <iostream>
    #include <iomanip>
    #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 = "";
    
      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<<"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 {
            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");
       return 0;
    }
    the bold lines are where I changed some things.
    like you can see I just isnerted some endlines (like I told you to do in my last post)
    I also added one more line, because the first line in your file is the filename, and not the firstname, but that doesnt have anything to do with your problem.

  9. #24
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    That works great...one final question...how do you completely delete the file, i used ios::trunc but that only deletes the information in the file, not the whole file itself.

  10. #25
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I'm pretty sure you can't do that with fstream.

    cstdio has a function called remove().

    Code:
     remove(filename);
    It returns a 0 if the file was successfully deleted. Just make sure you have write access to the file. Also, please understand that this won't just throw it into a recycling bin, it will completely remove it and you won't get it back. Just be careful what you test it on.
    Last edited by SlyMaelstrom; 11-14-2005 at 10:05 AM.
    Sent from my iPadŽ

  11. #26
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ok thanks

    I dont really want it to return a 0 so i will just leave it as it is unless anyone else knows anything.

  12. #27
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What do you mean you don't want it to return a zero? That's an error check, it doesn't mean anything to the program cosmetically.

    ...
    ...what?
    Sent from my iPadŽ

  13. #28
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I thought it would close the programme if you returned 0, i dont want to close the programme at that point

  14. #29
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No no. A return value is something that a function produces to pass back to the program running it. The return value will be in the form of whatever type the function is declared as, ie. if the function is declared an int, it will return an integer. If it's declared void, you don't need to return anything (though it's still customary to type return; at the bottom).

    Main is also a function. You can pass it parameters and it has a return value. ...but the return value doesn't close the program, it just returns a value to whatever is running it. In the case of main, it returns a exit success value, this is also what remove() returns, except it returns it for it's function. It has nothing to do with main or closing the program.
    Sent from my iPadŽ

  15. #30
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ok but I tried remove() and it doesnt seem to completely remove the file

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