Thread: Dynamic filenames?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    Dynamic filenames?

    Well, as anyone reading this probably knows, I'm working on this address book. It's going great now that I've realized some simple things, but this is the first (productive) time I've ever used file streams.

    I understand that to open a file for writing you use -
    ofstream::open(const char * filename, openmode mode);

    What confuses me mostly about this is const char *.
    Maybe I haven't been paying attention to myself when I've been teaching myself, (sorta like when I was in highschool...) but I can't figure out how to make that filename dependant on a variable of the current object.

    I have a feeling it probably has to do with string streams, which I didn't expect to have to get into.. However, I made the last lesson of mine (sorting vectors) a lot more complicated than it had to be, so maybe you guys can spit out a simple answer for me. =|

    Sorry if it seems like I'm constantly spamming the board with n00bish questions, but I plan to be an active member of this board for quite awhile. Plus, since I couldn't find an answer to my question
    on the board (maybe I didn't look hard enough, but I checked the tutorial and FAQ) this should become a decent reference for anyone else having the same problems.

    listName is of type std::string and I fear this is what's causing all the problems.

    Incase what I need answered needs to be explained differently, the code I tried to write looked like this (assuming fout is valid instance of ofstream):
    Code:
                string fileName=(listName+".dat");
                fout.open(fileName);
    The compiler basically told me filename has to be of type const char*. (I found this out by going to another programming site on file streams that showed the declaration of the open function.)

    I tried this next:
    Code:
                char *tempFileName=(strcat(listName[],".dat"));
                const char *fileName=tempFileName;
                fout.open(fileName);
    I thought maybe you couldn't set a constant as having a value dependant on the calling of a function, so -


    Ok.....I was browsing the web for answers and experimenting while writing this, and I found a solution. Everything in that last bit of code now seems butchered to me, and I realize why it didn't work. =P

    There's two different types of strings, which I was entirely unaware of. (I never touched C, I just started with C++. Is this a bad thing?) A C string and a C++ string. A C string is simply an array of characters, accessed by a pointer to the first character (I think), whereas a C++ string is a class (or something like it).

    You have to convert a regular (C++) std::string to a C string before it can be used in fstream::open() (Apparently C strings have the type const char*, which is what fstream::open() takes as it's first parameter). There is a member function of string that does that:
    std::string::c_str()
    I didn't look at how it works yet, but I probably should. Anyways, the return value is a pointer to a C string (constant pointer to the first character in the array?) that is identical to the value of the C++ string (which would be the current string; in my example listName).

    This is the code that works:
    Code:
                ofstream fout; //File output stream is fout
                string fileName=(listName+".dat"); //fileName is "listName.dat"
                fout.open(fileName.c_str()); //change fileName to C string and open it
    I wouldn't normally even bother posting this since I found the answer but I didn't feel like erasing it since it was so much typing. Might as well add it to the board. =P Hopefully it helps someone.

    One thing that could use answering though is, since filestreams are the C++ method of doing things, why don't they overload another fstream::open() function that recieves a string as it's first parameter? Wouldn't this ensure compatibility with C++ strings? I mean, why revert back to the old style of C strings when you can just use a variable that is a string instead of a pointer to the first character in an array? Out with the old! In with the new! =P

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Congratualtions on finding the answer... using .c_str() is correct.
    Quote Originally Posted by Callith
    I never touched C, I just started with C++. Is this a bad thing?
    No, but sometimes you will be confused by parts of C++ that are based on C code that isn't used as often in C++, like this. I remember reading a reasoning behind not overloading the file stream open functions to take a string, but I don't know if it was a good reason or if they plan on changing that in the future.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>I never touched C, I just started with C++. Is this a bad thing? No. (at least not in my opinion.)

    when passing variables to functions the compiler will change non-const variables to const variables if the function requires a const and what you passed isn't similar to the way the compiler will change variables to references for you, [though it won't change variables to pointers---oh well], if you pass a variable and the compiler is expecting a reference. .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array/vector help
    By Cpro in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2008, 03:30 PM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Dynamic Toolbars
    By nvoigt in forum Windows Programming
    Replies: 1
    Last Post: 01-11-2002, 10:21 AM