转换变量 [英] converting variables

查看:52
本文介绍了转换变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oakley解析器,并希望在日志文件中调用一个fopen函数来读取
。我可以使用它来读取文件,但前提是我在
下传递一个const char。变量为fopen。由于我希望结束

用户指定日志文件的名称,我在那里有一个scanf

允许用户这样做,但它不是'那时候是常数。我已经看到了参考像static_cast,dynamic_cast和

reinterpret_cast这样的调用,这将允许我转换为另一种类型,

但是我使用它们有点麻烦。


这是我试过的,没有运气:


printf("输入日志文件名称:");


scanf("%c",& filename);


reinterpret_cast< const char>(文件名);


并收到此错误:


OakleyParser.cpp(14):错误C2440:''reinterpret_cast'':不能

从''char''转换为''char''


因此VS.Net将两者识别为Char,但使用char变量

?文件名''在fopen中,我必须有一个常量。


file_in = fopen(filename," r");

OakleyParser.cpp(20):错误C2664:''fopen'':无法将参数

1从''char'转换为''cons' t char *''


有什么想法吗?

解决方案

Nate写道:

我正在使用Oakley解析器,并希望调用一个fopen函数来读取日志文件。我可以使用它来读取文件,但前提是我传递了一个const char。变量为fopen。由于我希望结束
用户指定日志文件的名称,我在那里有一个scanf允许用户这样做,但它不是那时的常量。我已经看到在C中看到像static_cast,dynamic_cast和
reinterpret_cast这样的调用,这将允许我转换为另一种类型,




没有那些''是C ++(C-plus-plus)的东西。在

相应的新闻组中重新发布您的问题。


案例


文章< 7a ************************** @ posting.google.com>,

Nate写道:< blockquote class =post_quotes>我正在使用Oakley解析器,并希望调用一个fopen
函数来读取日志文件。我可以使用它来读取
文件,但前提是我传递了一个const char文件。变量fopen。
由于我希望最终用户指定日志文件的名称,我在那里有一个scanf允许用户这样做,但
它不是'那时候是常数。我已经看到在C中引用了类似static_cast,dynamic_cast和reinterpret_cast的调用,
将允许我转换为另一种类型,但我在使用它们时遇到了一些麻烦。




你不需要施放它。将一个不合格的

指针传递给期望一个const指针的函数是可以的。


-

Neil Cerutti

这个野蛮人坐在靠近粪便一侧的凳子上,露出了他的身体,裸露了一条腰布,挥舞着长长的钢丝布

剑...... - 氩之眼


na**@nateharris.com (Nate )在留言新闻中写道:< 7a ************************** @ posting.google。 com> ...

printf("输入日志文件名:");

scanf("%c",& filename );


您需要在printf之间留出更多空间。和scanf。他们键盘

运营商需要一些时间才能输入信息。

运算符越慢,行之间的空间就越大。

scanf("%c"& filename);
reinterpret_cast< const char>(filename);


再次,你需要在这两者之间留出更多空间。

这就像重新解释历史一样。首先你应该在当前事件成为历史之前通过几年

然后你可以重新解释它。

并得到这个错误:

OakleyParser .cpp(14):错误C2440:''reinterpret_cast'':无法从''char''转换为''char''


reinterpret_cast意在改变_data type_而不是限定符。

const是一个数据限定符。尝试查找const_cast。

因此VS.Net将两者识别为Char,但要在fopen中使用char变量
?filename'',我必须有一个常量。


您应该能够将(char *)传递给函数expectng

(const char *),但不是相反。

有什么想法吗?




扔掉程序去钓鱼吧!


I am working on an Oakley parser and want to call an fopen function to
read in the log file. I can use this to read the file, but only if I
pass a "const char" variable to fopen. Since I would like the end
user to specify the name of the log file, I have a scanf in there to
allow the user to do so, but it isn''t a const at that point. I have
seen reference to calls like static_cast, dynamic_cast and
reinterpret_cast in C that will allow me to convert to another type,
but I am having a little trouble using them.

This is what I tried, with no luck:

printf("Enter the log file name: ");

scanf ("%c", &filename);

reinterpret_cast <const char>( filename );

and get this error:

OakleyParser.cpp(14) : error C2440: ''reinterpret_cast'' : cannot
convert from ''char'' to ''char''

So VS.Net recognizes both as Char, but to use the char variable
?filename'' in fopen, I have to have a constant.

file_in = fopen(filename, "r");

OakleyParser.cpp(20) : error C2664: ''fopen'' : cannot convert parameter
1 from ''char'' to ''const char *''

Any ideas?

解决方案

Nate wrote:

I am working on an Oakley parser and want to call an fopen function to
read in the log file. I can use this to read the file, but only if I
pass a "const char" variable to fopen. Since I would like the end
user to specify the name of the log file, I have a scanf in there to
allow the user to do so, but it isn''t a const at that point. I have
seen reference to calls like static_cast, dynamic_cast and
reinterpret_cast in C that will allow me to convert to another type,



No those''re C++ (C-plus-plus) things. Repost your question in the
appropriate newsgroup.

Case


In article <7a**************************@posting.google.com >,
Nate wrote:

I am working on an Oakley parser and want to call an fopen
function to read in the log file. I can use this to read the
file, but only if I pass a "const char" variable to fopen.
Since I would like the end user to specify the name of the log
file, I have a scanf in there to allow the user to do so, but
it isn''t a const at that point. I have seen reference to calls
like static_cast, dynamic_cast and reinterpret_cast in C that
will allow me to convert to another type, but I am having a
little trouble using them.



You do not need to cast it. It''s OK to pass an unqualified
pointer to a function expecting a const pointer.

--
Neil Cerutti
"The barbarian seated himself upon a stool at the wenches side, exposing
his body, naked save for a loin cloth brandishing a long steel broad
sword..." --The Eye of Argon


na**@nateharris.com (Nate) wrote in message news:<7a**************************@posting.google. com>...

printf("Enter the log file name: ");

scanf ("%c", &filename);
You need to give more space between "printf" and "scanf". They keyboard
operator will take some time to enter the information. The slower the
operator the more space should be given between the lines.
scanf ("%c", &filename);

reinterpret_cast <const char>( filename );
Again, you need to give more space between these two.
It is like reinterpreting history. First you should let a few years
pass before current events become history and then you can reinterpret it.
and get this error:

OakleyParser.cpp(14) : error C2440: ''reinterpret_cast'' : cannot
convert from ''char'' to ''char''
reinterpret_cast is meant to change the _data type_ and not qualifiers.
const is a data qualifier. Try looking up "const_cast".
So VS.Net recognizes both as Char, but to use the char variable
?filename'' in fopen, I have to have a constant.
You should be able to pass a (char *) to a function expectng
(const char *) but not the other way around.
Any ideas?



Throw away the program and go FISHING!


这篇关于转换变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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