Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Confused with error using ofstream

Reply
Thread Tools

Confused with error using ofstream

 
 
rEvolution27@gmail.com
Guest
Posts: n/a
 
      03-03-2007
I'm a c++ newbie here, trying out some stuff and when I try to compile
this:

void create() {
char name;
cout << "Creating a new timetable /n Please type a name for this
timetable";
cin >> name;
ofstream editFile;
editFile.open (name, ios:ut | ios::app);
editFile << name << endl;
}

I get this:
invalid conversion from `char' to `const char*'

I kinda understand why i'm getting this since when I change:
editFile.open (name, ios:ut | ios::app);
To:
editFile.open ("Timetable.txt", ios:ut | ios::app); , it compiles
But how do I get it do do what I want?

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-03-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm a c++ newbie here, trying out some stuff and when I try to compile
> this:
>
> void create() {
> char name;
> cout << "Creating a new timetable /n Please type a name for this
> timetable";
> cin >> name;
> ofstream editFile;
> editFile.open (name, ios:ut | ios::app);
> editFile << name << endl;
> }
>
> I get this:
> invalid conversion from `char' to `const char*'
>
> I kinda understand why i'm getting this since when I change:
> editFile.open (name, ios:ut | ios::app);
> To:
> editFile.open ("Timetable.txt", ios:ut | ios::app); , it compiles
> But how do I get it do do what I want?
>


char is a single char, a file name is (obviously) multiple chars.

Put it another way in C++ a char and a string of chars are not the same
thing.

The best way in C++ to do what you want is to use the string class. The
next best way is to use a dynamic array of chars, the worst way is to
use a static arrays of chars.

Here's some example code using the string class

#include <string>
#include <fstream>
using namespace std;

void create() {
string name;
cout << "Creating a new timetable \n Please type a name for this
timetable ";
cin >> name;
ofstream editFile(name.c_str());
editFile << name << endl;

You need the c_str() method to convert a C++ string into what ofstream
requires.

char, arrays of chars, dynamic allocation of arrays, strings etc. are a
big topic which should be covered in great detail in your favourite C++
book.

john
 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      03-03-2007
(E-Mail Removed) wrote:
> I'm a c++ newbie here, trying out some stuff and when I try to compile
> this:
>
> void create() {
> char name;


In the above line you define 'name' as a char. That is, a single
character, 1 byte. Probably not what you want.

> cout << "Creating a new timetable /n Please type a name for this
> timetable";
> cin >> name;


Because, as previously mentioned, name is a char, you read exactly 1
character here.


> ofstream editFile;
> editFile.open (name, ios:ut | ios::app);


open expects a POINTER to the first character in an null-terminated
array of characters. If you don't understand pointers and their
relationship to arrays, now is the appropriate time to drop what you are
doing and go read about it.


> editFile << name << endl;
> }
>
> I get this:
> invalid conversion from `char' to `const char*'


Given the above explanation, this message might make sense now. 'name'
is a char, but open wants a pointer to a char.

>
> I kinda understand why i'm getting this since when I change:
> editFile.open (name, ios:ut | ios::app);
> To:
> editFile.open ("Timetable.txt", ios:ut | ios::app); , it compiles


When you use a string literal (e.g. "Timetable.txt"), you create a
null-terminated array of characters. And arrays can be implicitly
converted to a pointer to their first element (again, go read about
pointers and arrays if that is new to you), therefore open is getting
what it wants, a pointer to a character.

> But how do I get it do do what I want?
>


A number of ways. First, the not particularly safe, but easier to
understand way:

// I chose the size 80 arbitrarily. This is unsafe.
char name[80] ;
cin >> name ;

This will read from standard input until it sees white space. If there
are more than 79 characters (the null-terminator takes 1 character, read
about C-style strings if this is new to you) available then you have
your classic buffer-overflow problem.

A second, more complicated solution, would be to dynamically allocate an
array, and resize it each time it runs out of space. This is not
trivial to do correctly, so I won't even bother. Let's move on to the
third solution.

Use std::string.

#include <string>

....

std::string name ;
cin >> name ;

....

editFile.open(name.c_str(), ios:ut | ios::app) ;


std::string does any necessary memory allocating and reallocating for
you. You can get pointer to a null-terminated array of characters using
the c_str method, as demonstrated above.

--
Alan Johnson
 
Reply With Quote
 
rEvolution27@gmail.com
Guest
Posts: n/a
 
      03-03-2007
Ok, thanks.
For some reason I thought 'char name' would create a character array
that matches the size of what's put into it. I guess i'll use strings
then.

 
Reply With Quote
 
rEvolution27@gmail.com
Guest
Posts: n/a
 
      03-03-2007
How would I make it so that the string can accept more than one word?

I've got this so far...
void create() {
string name;
cout << "Creating a new timetable"<< endl <<"Type a name for this
timetable" << endl;
cin >> name;
ofstream editFile;
editFile.open ("timetable.txt", ios:ut | ios::trunc);
editFile << name << endl;
}

But when I open timetable.txt, I only see the first word of what I
typed for name.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-03-2007
(E-Mail Removed) wrote:
> How would I make it so that the string can accept more than one word?
>
> I've got this so far...
> void create() {
> string name;
> cout << "Creating a new timetable"<< endl <<"Type a name for this
> timetable" << endl;
> cin >> name;
> ofstream editFile;
> editFile.open ("timetable.txt", ios:ut | ios::trunc);
> editFile << name << endl;
> }
>
> But when I open timetable.txt, I only see the first word of what I
> typed for name.
>

That's how std::istream& operator( std::istream& std::string& ) works.
If you want the full line, use getline.

--
Ian Collins.
 
Reply With Quote
 
rEvolution27@gmail.com
Guest
Posts: n/a
 
      03-03-2007
On Mar 3, 6:04 pm, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > How would I make it so that the string can accept more than one word?

>
> > I've got this so far...
> > void create() {
> > string name;
> > cout << "Creating a new timetable"<< endl <<"Type a name for this
> > timetable" << endl;
> > cin >> name;
> > ofstream editFile;
> > editFile.open ("timetable.txt", ios:ut | ios::trunc);
> > editFile << name << endl;
> > }

>
> > But when I open timetable.txt, I only see the first word of what I
> > typed for name.

>
> That's how std::istream& operator( std::istream& std::string& ) works.
> If you want the full line, use getline.
>
> --
> Ian Collins.


You lost me there....

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-03-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Mar 3, 6:04 pm, Ian Collins <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > How would I make it so that the string can accept more than one word?

>>
>> > I've got this so far...
>> > void create() {
>> > string name;
>> > cout << "Creating a new timetable"<< endl <<"Type a name for this
>> > timetable" << endl;
>> > cin >> name;
>> > ofstream editFile;
>> > editFile.open ("timetable.txt", ios:ut | ios::trunc);
>> > editFile << name << endl;
>> > }

>>
>> > But when I open timetable.txt, I only see the first word of what I
>> > typed for name.

>>
>> That's how std::istream& operator( std::istream& std::string& ) works.
>> If you want the full line, use getline.
>>
>> --
>> Ian Collins.

>
> You lost me there....


std::getline( std::cin, name );


 
Reply With Quote
 
rEvolution27@gmail.com
Guest
Posts: n/a
 
      03-04-2007
The following compiles, but the getline doesn't seem to work... am I
missing something here?

void create() {
string name;
cout << "Creating a new timetable"<< endl <<"Type a name for this
timetable" << endl;
getline(cin,name);
ofstream editFile;
editFile.open ("timetable.txt", ios:ut | ios::trunc);
editFile << name << endl;
system ("pause");





 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-04-2007
(E-Mail Removed) wrote:
> The following compiles, but the getline doesn't seem to work... am I
> missing something here?
>
> void create() {
> string name;
> cout << "Creating a new timetable"<< endl <<"Type a name for this
> timetable" << endl;
> getline(cin,name);
> ofstream editFile;
> editFile.open ("timetable.txt", ios:ut | ios::trunc);
> editFile << name << endl;
> system ("pause");
>
>
>
>
>


Can't tell from the code you've posted, but possibly you've fallen into
the well known gotcha described here

http://www.parashift.com/c++-faq-lit....html#faq-15.6

In other words getline is working, it's just it is reading the newline
that was left behind by some previous input.

john
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compilation error using ofstream aaragon C++ 15 09-28-2007 11:44 AM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
ofstream error conditions jois.de.vivre@gmail.com C++ 3 06-03-2005 12:42 AM
Error using ofstream close() Colum C++ 2 12-19-2003 12:43 PM
ofstream file error checks? steve C++ 2 08-20-2003 12:28 AM



Advertisments