Skip to main content
GameDev.net gamedev.net
🔒 Locked

opening a file using a string?

Started by polisasimo Jul 31, 2005 at 10:42 PM 9 replies 1.2k views
Original Post
polisasimo
polisasimo
and yet another setback : I wish to open a file using a string called file but I am getting stuck using this code and canot figure out why. cout << "Enter the name of the file you would like to open: "; cin >> file; stringFile.open(file); stringFile >> list1; any ideas would be helpful.
Oluseyi
Oluseyi
Error messages would be helpful. (I no longer bother taking shots in the dark.)
polisasimo
polisasimo
here is the error sorry


c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\P5150\linkedList_driver.cpp(26): error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const char *,std::_Iosb<_Dummy>::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits,
_Dummy=int
]
Strewth
Strewth
You'll need to use .c_str() on your string filename:

stringFile.open(file.c_str());
Blake's 7 RPG - a new project
polisasimo
polisasimo
Ok that worked like a champ converting to a c-string but I do not understand why that is the case.


regaurdless thanks for the help and if anyone cares to explain why that would be awesome :)
Roboguy
Roboguy
Quote:
Original post by polisasimo
Ok that worked like a champ converting to a c-string but I do not understand why that is the case.


regaurdless thanks for the help and if anyone cares to explain why that would be awesome :)


The std::string::open method only takes a const char*, not a std::string.
polisasimo
polisasimo
Thanks for all the help I guess that is just one of those things a begining programmer has to accept :)
MaulingMonkey
MaulingMonkey
Quote:
Original post by polisasimo
Thanks for all the help I guess that is just one of those things a begining programmer has to accept :)


Yep. If the creators of the SC++L had wanted to, they could have provided additional constructors for std::fstream so your original example would work. I believe the rationale against this was the fact that doing this would force anyone linking against the iostream portion of the standard library to also link against the string portion.

The important part of your error message was "cannot convert parameter 1 from 'std::string' to 'const char *'". "const char *" is usually a pointer to an old C-style ASCIZ (ASCII terminated with a Zero) string. These can be/are slow to manipulate [JoS] and should be avoided when possible in your own code (or in the 1 in a million occasion where you really need that extra millisecond of optimization, used very carefully with expert knowledge along with profiling).

However, one must still occasionally use ASCIZ style strings, usually when working with libraries, badly written or prestandard C++ code, or on occasion due to targeting an embeded platform (e.g. old PDAs) which don't (fully) implement the standard library.

Because of this, the C++-style std::string provides the function c_str(), which returns a constant pointer to an ASCIZ style string. If the original string is modified, memory may be reallocated, invalidating the pointer. As such, it's usually wise to avoid storing this pointer.

Just my 2¢

-Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.