使用ofstream混淆错误 [英] Confused with error using ofstream

查看:81
本文介绍了使用ofstream混淆错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这里的c ++新手,尝试了一些东西,当我尝试编译

时:


void create(){

char name;

cout<< 创建一个新的时间表/ n请输入此

时间表的名称;

cin> name;

ofstream editFile; < br $> b $ b editFile.open(name,ios :: out | ios :: app);

editFile<<名称<<结束;

}


我得到这个:

无效从`char''转换为`const char *''


我有点理解为什么自从我改变以来我得到这个:

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

要:

editFile.open(" Timetable.txt",ios :: out | ios :: app); ,它编译

但是如何让它做我想做的事情?

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?

推荐答案

rE ********** @ gmail.com 写道:

我这里是c ++新手,尝试一些东西,当我尝试编译

时:


无效create(){

char name;

cout<< 创建一个新的时间表/ n请输入此

时间表的名称;

cin> name;

ofstream editFile; < br $> b $ b editFile.open(name,ios :: out | ios :: app);

editFile<<名称<<结束;

}


我得到这个:

无效从`char''转换为`const char *''


我有点理解为什么自从我改变以来我得到这个:

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

要:

editFile.open(" Timetable.txt",ios :: out | ios :: app); ,它编译

但是如何让它做我想做的事?
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是一个字符,文件名是(很明显)多个字符。


换句话说,在C ++中,一个字符和一串字符不一样

的东西。


C ++中你想做的最好的方法是使用字符串类。

下一个最好的方法是使用动态数组的字符,最糟糕的方式是使用静态数组字符来使用



这里是一些使用字符串类的示例代码


#include< string>

#include< fstream>

using namespace std;


void create(){

string name;

cout<< 创建一个新的时间表\ n请输入此

时间表的名称;

cin> name;

ofstream editFile (name.c_str());

editFile<<名称<< endl;


您需要使用c_str()方法将C ++字符串转换为
需要的内容。


char,字符数组,数组的动态分配,字符串等都是一个很大的话题,应该在你最喜欢的C ++

书中详细介绍。 />

john

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


rE**********@gmail.com 写道:

我在这里是c ++新手,尝试了一些东西和当我尝试编译

时:


void create(){

char name;
I''m a c++ newbie here, trying out some stuff and when I try to compile
this:

void create() {
char name;



在上面的行中,您将''name''定义为char。也就是说,单个

字符,1个字节。可能不是你想要的。

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


cout<< 创建新的时间表/ n请输入此

时间表的名称;

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



因为,如前所述,name是一个字符,你在这里读到的确是1

字符。

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


ofstream editFile;

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



open要求指向空终止的

字符数组中第一个字符的POINTER。如果你不理解指针和它们与阵列的关系,现在是适当的时间放弃你所做的b $ b做的事情并去了解它。

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<<名称<<结束;

}


我得到这个:

无效从`char''转换为`const char *''
editFile << name << endl;
}

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



鉴于上述说明,此消息现在可能有意义。 ''name''

是一个char,但open想要一个指向char的指针。

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


>

我有点理解为什么自从我改变以来我得到这个:

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

要:

editFile.open(" Timetable.txt",ios :: out | ios :: app); ,它编译
>
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



当你使用字符串文字(例如Timetable.txt)时,你创建一个

以null结尾的数组的人物。并且数组可以隐式地将
转换为指向其第一个元素的指针(再次,阅读关于

指针和数组,如果这对你来说是新的),因此打开了

它想要什么,一个指向角色的指针。

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?



多种方式。首先,不是特别安全,但更容易理解方式:


//我随意选择尺码80。这是不安全的。

char name [80];

cin> name;


这将从标准输入读取,直到它看到了白色的空间。如果

超过79个字符(空终止符需要1个字符,请阅读

关于C风格的字符串,如果这对你来说是新的)可用,那么你有

你的经典缓冲区溢出问题。


第二个更复杂的解决方案是动态分配一个

数组,并且每次空间不足时调整大小。这不是正确的做法,所以我甚至都不会打扰。让我们转到

第三个解决方案。


使用std :: string。


#包括< string>


....


std :: string name;

cin>姓名;


....


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

std :: string为你分配和重新分配

。您可以使用

c_str方法获取指向以null结尾的字符数组的指针,如上所示。


-

Alan Johnson

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::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


好的,谢谢。

出于某种原因,我认为''char name''会创建一个字符数组

匹配放入其中的大小。我想我会用字符串

然后。

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.


这篇关于使用ofstream混淆错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆