Thread: taking user input as title for output file...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    taking user input as title for output file...

    Hi,

    Is it possible to take the user input ( say if the user were to type their name) and then take this and create a file using ofstream?

    I can only create files when I do -

    ofstream recipt("title.txt", ios::ate);



    If you need more info please ask

    Cheers

    PK

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes that's possible. Try something like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    cout<<"Enter your name:";
    char name[256]={0};
    cin>>name;
    strcat(name,".txt");
    ofstream afile(name);
    afile.close();
    cout<<endl<<name<<" successfully created.\n";
    cin.get();
    return 0;
    }
    Didn't test that but it should work.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    This seems familiar...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Here you go try this:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
         char filename[56];
         char filepath[200]="(filepath goes here ex. C:/My Documents/...)";
         cout<<"Name: ";
         cin.getline(filename, 56);
         strcat(filepath, filename);
         strcat(filepath, ".txt");
         ofstream fout(filepath)
         //fout any information you need here
         fout.close();
         cout<<"DONE";
         cin.ignore();
         return 0;
    }
    Good Luck!
    Last edited by Junior89; 12-25-2005 at 09:21 PM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    OOO

    Thanks for the replies!!!
    It helped alot!!!

    Done ALOT on my coursework today!!! Stoped working at about 11pm At which time I am usuaully slacking off

    Anywhoo - cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM