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):
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:
I thought maybe you couldn't set a constant as having a value dependant on the calling of a function, so -
const char *fileName=tempFileName;
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:
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:
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.
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
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