By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

page 293 of C++ primer,4th edition

P: n/a
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}

Sep 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
add

using namespace std;

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"asdf" <li*********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}

Sep 18 '06 #2

P: n/a
asdf wrote:
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
main returns int. Always.
{
ofstream outfiloe(ofile.c_str());
What is ofile? Where did you define it? Without the definition, this
is an error.

ofstream resides in the std namespace, so you need to qualify it with
"std::".

Best regards,

Tom

}
Sep 18 '06 #3

P: n/a
"asdf" <li*********@gmail.comwrote:
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}
Moonlit wrote:
add

using namespace std;
Do you believe that adding that will fix the program?

Best regards,

Tom

Sep 18 '06 #4

P: n/a

"Moonlit" <news moonlit xs4all nlwrote in message
news:45**********************@news.xs4all.nl...

Please don't top-post. Place responses after (or interspersed with) what
you're responding to. [re-arragned]

>how to use c_str() ?
It's a member of std::string. Look it up.
>>
if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
int main()
>{
ofstream outfiloe(ofile.c_str());
What's "ofile"?
>}
add

using namespace std;
Ron, you didn't say where to add that. But better, in such a simple case,
would be to add the std:: specifiers where needed, or to add using
statements for the specific std:: items that will be used.

But, there's more here wrong than just that.

What's "ofile"? It's not defined. I'm guessing it's supposed to be a
std::string, given the use of c_str(), but I don't see it declared as such
anywhere. And the <stringheader needs to be included if using strings.
Also, the string needs to get its content filled somehow, such as by using
std::cin.

If this was an attempt at reducing what you're posting, you cut out too
much. Read the FAQ, at http://www.parashift.com/c++-faq-lite/ (especially
section 5, reagarding how to post here for best results).

And if you're getting errors you don't understand, post the error message
(the text; error numbers mean nothing for people using other compilers), and
tell us which line of code generates the error.

Lastly, most of us probably don't have the book referred to in your subject
line. Make your subject lines more specific to the problem (such as "error
using c_str()" or "how do I use c_str()?"). In the body of your message,
you can refer to the book you're getting an example from (but don't expect
we'll all have that book).

-Howard
Sep 18 '06 #5

P: n/a
Moonlit wrote:
add
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Sep 18 '06 #6

P: n/a
asdf wrote:
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}
In the future, post complete programs. Do that by copying the actual
code, not retyping. Also include the exact error messages.


Brian
Sep 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.