Thread: Saving Variable File Name

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Saving Variable File Name

    Hello all!

    I want to ask the user a question and when the user answers it makes a file with the name of the answer I.E:

    "What is your name?"
    User inputs "Fred"
    Then a text file with the name of your answer will be written. So it would be called "Fred.txt".

    Is this even possible? If so help would be greatly appreciated.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure. You save the user's input in a variable and pass that variable to the function that creates the file. Quite a silly question, actually. If it wasn't possible, what kind of inflexible language would it be then?
    Anyway, let's start by asking what you know. Do you know how to open files, for example? And do you know how to input data from the user?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    What I know.

    Here's my 2 codes for asking a users name and writing a file:

    User Name input:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    void myflush(std::istream& in)
    {
       in.clear();
       in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    void mypause()
    {  
       myflush(std::cin);
       std::cin.get();
    }
    int main(void)
    {
       string name;          
                        
       cout<<"What's your name?";    
       std::getline(cin, name);        
       cout<<"Hi! " << name;
       cin.get();     
       mypause();
    }



    Write a file:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    void myflush(std::istream& in)
    {
        in.clear();
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    void mypause()
    {  
        myflush(std::cin);
        std::cin.get();
    }
    int main(void)   
    {
       cout << endl;
       ofstream a_file ( "example.txt" );
       a_file << "Example.";
       a_file.close();
       mypause();
     }
    Last edited by Apocalyptic_end; 01-26-2008 at 03:55 PM.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok. So where would I go to try to learn this. I've googled it and I couldn't find it.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wait. My bad. Haha. Sorry.
    So you understand how to open files and how to ask for input.
    Then you should simply merge them.
    Ask for the filename, then pass that filename to the open function.
    Just note that since you're reading into a std::string, you should call .c_str() because they take const char*.

    Code:
    std::string mystr = "test";
    const char* p = mystr.c_str();
    Last edited by Elysia; 01-26-2008 at 05:48 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    const char*
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok... so where do I put that code in my program and what code do I merge?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you understand what your code does, it should not be difficult.
    Get input.
    Open file using input.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    I don't want to Open a file, I want to write it, the Input variable will be the name of it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To write to a file, you must open it first (with writing permission). So open it with the inputted filename.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Reading lines from a file and saving to a variable
    By Rare177 in forum C Programming
    Replies: 1
    Last Post: 06-09-2004, 03:47 PM