473,549 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused with error using ofstream

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::out | 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::out | ios::app);
To:
editFile.open ("Timetable.txt ", ios::out | ios::app); , it compiles
But how do I get it do do what I want?

Mar 3 '07 #1
15 2453
rE**********@gm ail.com 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::out | 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::out | ios::app);
To:
editFile.open ("Timetable.txt ", ios::out | 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
Mar 3 '07 #2
rE**********@gm ail.com 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::out | 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::out | ios::app);
To:
editFile.open ("Timetable.txt ", ios::out | 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(n ame.c_str(), ios::out | 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
Mar 3 '07 #3
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.

Mar 3 '07 #4
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::out | ios::trunc);
editFile << name << endl;
}

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

Mar 3 '07 #5
rE**********@gm ail.com 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::out | 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.
Mar 3 '07 #6
On Mar 3, 6:04 pm, Ian Collins <ian-n...@hotmail.co mwrote:
rEvolutio...@gm ail.com 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::out | 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....

Mar 3 '07 #7
<rE**********@g mail.comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
On Mar 3, 6:04 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>rEvolutio...@g mail.com 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::out | 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 );
Mar 3 '07 #8
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,nam e);
ofstream editFile;
editFile.open ("timetable.txt ", ios::out | ios::trunc);
editFile << name << endl;
system ("pause");

Mar 4 '07 #9
rE**********@gm ail.com 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,nam e);
ofstream editFile;
editFile.open ("timetable.txt ", ios::out | 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
Mar 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4088
by: Laxman | last post by:
==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <==
2
10562
by: steve | last post by:
Is there a way to catch file errors while writing to a file using ofstream? For ex., if the file is deleted or permissions changed by another process after it is opened, or when the disk is full. I couldn't figure out which members could be used to check for these types of errors. I tried the good(), bad(), fail() etc., after writing to...
6
1884
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of this function. bool processdrecord( ofstream& prnfile, ofstream& validdata, char* record ) { drecord Newdrecord; char customercode;
11
4172
by: muser | last post by:
In the code I supplied before this one, the cause of the problem is an access violation error. When I run the debugger it skips into what I can only assume is the compilers version of my code. And I don't know where the problem lies in the code. Can anyone run it through there own compiler please and tell me where and what the problem is. ...
3
9083
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++ functions that do this or is there any way I can check manually? Thanks, Prashant
2
2742
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
24
2114
by: Noah Roberts | last post by:
Item #1 in the More Exceptional C++ book uses the following construct: fstream in; .... process( in.is_open() ? in : cin,...); Where process has been shown as having various multiple different
7
2125
by: teddarr | last post by:
I have an assignment I've been working on for a few days now. I need some help with the last detail. The program is supposed to create 5 objects with varying floating-point parameter lists. The class (Rectangle) is supposed to calculate the perimeter and area and tell whether the shape is a square or not. Then put this info in a table and...
15
5981
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .... std::ofstream fout; fout.open("hello.out"); fout<<setfill('-')<<setw(20)<<"-"<<setfill(' ')<<endl; fout<<"hello "; // this is line 139
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.