Thread: String as Filenames

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    97

    String as Filenames

    i was wondering if you can have the file name the file ofstream makes when you run your program a string. like if the user wanted to have there own name for the file. thx

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You mean like this:
    Code:
    std::string FileName;
    std::ofstream File;
    
    ...
    
    std::cout << "Enter a filename: ";
    std::cin >> FileName;
    
    ...
    
    File.open(FileName.c_str(), std::ios::out);
    if(!File.fail())
    {
       ...
    
       File.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    ummm....maybe, ur code is extremely hard to read. i just want to know if u can have a string be a filename

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i just want to know if u can have a string be a filename
    Yes.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    so would it be like this:?

    [code]
    char name[50]
    cout << "Please enter the filename title: ";
    cin >> name;

    ofstream a_file(name)
    [code]

    ? i didnt include everything needed, like text to be put in the file. but is that how it would look?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but is that how it would look?
    More or less. Have you tried it yet to see if it works? Writing little test programs to see if your assumptions are correct is a useful habit to get into.
    My best code is written with the delete key.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by oobootsy1
    ummm....maybe, ur code is extremely hard to read. i just want to know if u can have a string be a filename
    isn't the filename by default a string? You mean have the user rename the file correct?
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    ....i dont have the internet where my computer is, so i only get on the internett at library and such. i mean have the filename a string!

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, your example is what it would look like, except you forgot the semicolon after "char name[50]" and after "ofstream a_file(name)". That's one way to do it. Although Magos' example is more fool-proof.

    Code:
    string name;
    cout << "Please enter the filename title: ";
    cin >> name;
    
    ofstream a_file(name.c_str());
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM