• Advertisement
Sign in to follow this  

opening a file using a string?

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Strings aren't used for opening files, file streams are. Try something like:

#include <fstream>
// ...

std::string fileName;

cout << "Enter the name of the file you would like to open: ";
cin >> fileName;

std::ifstream file(fileName);

file >> list1;

Share this post


Link to post
Share on other sites
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<char>,
_Dummy=int
]

Share this post


Link to post
Share on other sites
You'll need to use .c_str() on your string filename:

stringFile.open(file.c_str());

Share this post


Link to post
Share on other sites
Ah, you need to call std::string::c_str() to convert it to a const character array before passing it as an argument, like this:

stringFile.open(file.c_str());


EDIT: Beaten by 20 seconds!

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks for all the help I guess that is just one of those things a begining programmer has to accept :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement