Thread: File input/output, using a string as a filename?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    36

    File input/output, using a string as a filename?

    My program:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            string Goblox = "\"C:\\Program Files\\Machinery\\Goblox.txt\"";
    
            ofstream myfile1;
            myfile1.open (" Goblox.c_str() "); //Obviously, there's something wrong about this line
            myfile1<< Goblox;
            myfile1.close();
    }
    The string "Goblox" contains a filepath to a txt file not created yet. I want the program to create that file so it will be named f.x. Goblox.txt in the path the string contains using ofstream, but I don't know how to tell the program in the "myfile.open" line to create a file using the path contained in the string rather than creating a file "Goblox.c_str()".

    How do I do this?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    theres two things you need to fix here:
    Code:
    string Goblox = "\"C:\\Program Files\\Machinery\\Goblox.txt\"";
    // ...
    myfile1.open (" Goblox.c_str() ");
    looking at the open call, it will create a file called "Goblox.c_str() " in the current directory. to use the contents on the string 'Goblox', remove the quotes in the open call, so it looks like
    Code:
    myfile1.open(Goblox.c_str());
    now it will create a file with filename being the contents of the 'Goblox' string.

    lets see what the contents of this variable are:
    "C:\Program Files\Machinery\Goblox.txt"
    note this is the string produced by your declaration of the string. the "\\"s are of course replaced by "\", and the double quotes are include in the filename. im pretty sure you cant have " characters in a filename, so remove the two \" from the Goblox string.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    Strangely enough, this still doesn't work. The program compiles and runs without errors, but after running, "Goblox.txt" still doesn't get created in "C:\Program Files\Machinery\" ...

    I tried letting it being created in "C:\Goblox.txt\" but it still didn't work. Any help?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The file name probably doesn't contain these quotes:

    Code:
    "\"C:\\Program Files\\Machinery\\Goblox.txt\""
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    I got it now! All I had to do:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            string Goblox = "C:\\Program Files\\Machinery\\Goblox.txt"; // It seems it should not be quotes in this string, nor \ in the end. So, instead of: "\"C:\\Program Files\\Goblox\\\", it should be: "C:\\Program Files\\Goblox.txt"
    
            ofstream myfile1;
            myfile1.open ( Goblox.c_str() ); //Quotes removed
            myfile1<< Goblox;
            myfile1.close();
    }
    Thanks a lot!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of:
    Code:
    ofstream myfile1;
    myfile1.open ( Goblox.c_str() ); //Quotes removed
    Just write:
    Code:
    ofstream myfile1( Goblox.c_str() ); //Quotes removed
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Helgso View Post
    I got it now! All I had to do:
    ...
    Code:
    ...
            string Goblox = "C:\\Program Files\\Machinery\\Goblox.txt"; // It seems it should not be quotes in this string, nor \ in the end. So, instead of: "\"C:\\Program Files\\Goblox\\\", it should be: "C:\\Program Files\\Goblox.txt"
    ...
    im pretty sure thats what i told you to do!

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by laserlight View Post
    Instead of:
    Code:
    ofstream myfile1;
    myfile1.open ( Goblox.c_str() ); //Quotes removed
    Just write:
    Code:
    ofstream myfile1( Goblox.c_str() ); //Quotes removed
    Is it just me or do you guys often find it annoying that the fstream constructor can't accept a std::string?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is, as well as anything else that accepts (const) char*, but not std::string. Unfortunately, there is not much to do.
    Hopefully, it will be fixed in the next standard.
    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. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM