C++ ifstream error using string as opening file path.
up vote
62
down vote
favorite
I have:
string filename:
ifstream file(filename);
The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?
Here's the error:
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
c++ ifstream
add a comment |
up vote
62
down vote
favorite
I have:
string filename:
ifstream file(filename);
The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?
Here's the error:
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
c++ ifstream
1
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34
add a comment |
up vote
62
down vote
favorite
up vote
62
down vote
favorite
I have:
string filename:
ifstream file(filename);
The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?
Here's the error:
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
c++ ifstream
I have:
string filename:
ifstream file(filename);
The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?
Here's the error:
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
c++ ifstream
c++ ifstream
edited Oct 3 '12 at 20:48
KronoS
4,10784676
4,10784676
asked Jun 12 '11 at 18:08
Mark
3,112114166
3,112114166
1
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34
add a comment |
1
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34
1
1
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34
add a comment |
3 Answers
3
active
oldest
votes
up vote
126
down vote
accepted
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
add a comment |
up vote
11
down vote
The ifstream
constructor expects a const char*
, so you need to do ifstream file(filename.c_str());
to make it work.
add a comment |
up vote
2
down vote
in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.
while your answer is correct, it is already present in the accepted answer, although not so clearly:not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6323619%2fc-ifstream-error-using-string-as-opening-file-path%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
126
down vote
accepted
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
add a comment |
up vote
126
down vote
accepted
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
add a comment |
up vote
126
down vote
accepted
up vote
126
down vote
accepted
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
edited Oct 3 '12 at 23:18
answered Jun 12 '11 at 18:09
Seth Carnegie
60.2k14146213
60.2k14146213
add a comment |
add a comment |
up vote
11
down vote
The ifstream
constructor expects a const char*
, so you need to do ifstream file(filename.c_str());
to make it work.
add a comment |
up vote
11
down vote
The ifstream
constructor expects a const char*
, so you need to do ifstream file(filename.c_str());
to make it work.
add a comment |
up vote
11
down vote
up vote
11
down vote
The ifstream
constructor expects a const char*
, so you need to do ifstream file(filename.c_str());
to make it work.
The ifstream
constructor expects a const char*
, so you need to do ifstream file(filename.c_str());
to make it work.
answered Jun 12 '11 at 18:09
Alexander Gessler
38.6k569113
38.6k569113
add a comment |
add a comment |
up vote
2
down vote
in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.
while your answer is correct, it is already present in the accepted answer, although not so clearly:not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
add a comment |
up vote
2
down vote
in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.
while your answer is correct, it is already present in the accepted answer, although not so clearly:not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
add a comment |
up vote
2
down vote
up vote
2
down vote
in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.
in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.
answered Jan 5 '16 at 20:17
evi v
215
215
while your answer is correct, it is already present in the accepted answer, although not so clearly:not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
add a comment |
while your answer is correct, it is already present in the accepted answer, although not so clearly:not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
while your answer is correct, it is already present in the accepted answer, although not so clearly:
not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
while your answer is correct, it is already present in the accepted answer, although not so clearly:
not a string pre-C++11.
– Chris Maes
Jan 6 '16 at 8:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6323619%2fc-ifstream-error-using-string-as-opening-file-path%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12
I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13
That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34