Thread: File Output

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    File Output

    I am tyring to create a programme where it writes certain information to a certain file. How do you make the file that it writes equal to a string that is typed in by the user. What i Put is:
    Code:
    ofstream a_file (name".txt");

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Here is the block of code that the errorsare in:
    Code:
     else if (type == "create"){
            cout<<endl;
            cout<<"Please enter the name of the member: ";
            getline(cin, name);
            cout<<endl;
            ofstream a_file ( name".txt" );
            a_file<<name;
            cout<<"Please enter the member's first name: ";
         }
    That is obviously not finished though.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The istream and ofstream contructors require an argument that is a char*. When you read in the filename, what type of variable is it stored in? If it's a <string> you can use filename.c_str() to convert the string type to a char*.

    In addition, you don't join strings together doing this:
    Code:
    string str1 = "hello ";
    string str2 = str1"world";
    You do this:

    string str2 = str1 + "world";
    Last edited by 7stud; 11-13-2005 at 07:46 AM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thanks, would it be...filename.c_str( name )

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "filename" is the name of the string variable. If your variable is named "name", you wouldn't do this:

    name.c_str(name)

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How exactly do I fit it together then? Can you show me the code edited the way it should be?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Would that be possible? I am very stupid an dwould not understand otherwise.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try this:
    Code:
    string filename = "something";
    cout<<filename.c_str()<<endl;
    When you cout a string variable and then its character array equivalent(a C-style string), they will look exactly the same.
    Last edited by 7stud; 11-13-2005 at 03:02 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I don't want my string to be defined, i want it to be user defined and then that name is applied to the name of the output file that is created. here is the whole code it might be easier (it is not finished.)

    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 = "";
    
      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 name of the member: ";
            getline(cin, name);
            cout<<endl;
            ofstream a_file (name".txt");
            a_file<<name;
            cout<<"Please enter the member's first name: ";
         }
       }
       while (finish == "no");
    }

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Code:
    	cout << "Please enter the name of the member: ";
    	getline(cin, name);
    	name += ".txt";
    	ofstream a_file ( name.c_str() );

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well I dont get the errors anymore but I need to write the rest of the code to see if it works.

    Thanks

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I am having a new problem with showing the file created.

    Here is the code:

    Code:
    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");
               }
         }

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Oh, the problem is that it does not show the information about the person where it should, it just tellas you all the stuff in onelong line. Like: bumfluff.txtMichaelBamforth08/08/[email protected]

    and that is not what I want

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    A little help wouldnt go amiss.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    how about posting the contents of the file (exactly), and posting exactly what the output of the program is.

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