Thread: Creating multiple files with string as filename

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

    Creating multiple files with string as filename

    I'm attempting to create a bunch of files with strings as the variable name..

    Example : I get the name Firefox and then append it to the string ".url" to get Firefox.url that is saved as filename.

    ofstream name2;
    name2.open(filename);

    But it gives an error if I try to open a file name as a string. If I hard-coded "firefox.url" it would work however.

    Any way around this?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, there is no version of "open()" that takes a std::string - you can however use the member function c_str() to get a copy of the content as a "C string" (char array or pointer to char, however you wish to look at it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yeah, iow:
    Code:
    name2.open(filename.c_str());
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  2. Creating String Functions
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2006, 05:26 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  5. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM