converting variables

N

Nate

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?
 
C

Case

Nate said:
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
 
N

Neil Cerutti

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.
 
K

kal

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!
 
M

Mitchell

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

scanf ("%c", &filename);

reinterpret_cast <const char>( filename );


How did you declare filename?

char filename[1024];

or something similar? In this case, your scanf line should read

scanf("%s",filename);

because filename is already the address of the character array.
&filename points to the sky. Also, use %s !!!!! %c is for reading in a
character.
OakleyParser.cpp(14) : error C2440: 'reinterpret_cast' : cannot
convert from 'char' to 'char'

a=reinterpret_cast<char>(x);

is c++'s way of saying

a=(char) x;

for nonpolymorphic types.
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");

Oh, there's no need! =) Just feed it a char. It will be implicitly
casted to const char.

cheers!
 
N

Nick Keighley

(e-mail address removed) (Nate) wrote in message
first decide which language you are programming in C or C++. C has no
reinterpret_cast and a C++ program would be unlikely to use scanf() or
fopen(). I'll assume you want to do this in C.
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.

note: that's "const char *". In fact it will accept a "char *"
argument. (the "const" is a promise that the function won't modify the
argument).
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.

it isn't necessary
This is what I tried, with no luck:

printf("Enter the log file name: ");
scanf ("%c", &filename);

what is filename? It should an array of char.

char filename [1024];
...
scanf ("%s", filename);

or better, avoid scanf() (its error recovery is poor) and use fgets()
(you'll have to check for '\n' and embedded spaces.
reinterpret_cast <const char>( filename );

and get this error:

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

don't do this.
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 *'

it's not the "const" that's the problem. Declare filename correctly
and all your problems go away.
Any ideas?

don't confuse char* with char
 
N

Nick Keighley

Mabden said:
What's wrong with embedded spaces?

the scanf() solution won't have embedded spaces (as someone else pointed out
%s stops at the first whitespace). Whilst fgets() reads the whole line (or
as much of it as it has room for) including spaces. I was hinting that
fgets() isn't identical to scanf(). In a real program you might to check
it's a valid filename (control characters allowed?). It depends on your OS
what's allowed and what is sensible.
 
S

Sam Dennis

Nick said:
[for reading a line]
scanf ("%s", filename);

int result = scanf( "%" STR( FILENAME_MAX ) "[^\n]", filename );
or better, avoid scanf() (its error recovery is poor) and use fgets()

Actually, when used in a few certain ways, it can replace fgets and
simplify error checking at the cost of a little hassle if one needs
to change the field width(s). (This would be a much more difficult
decision if fgets returned the number of characters read; as things
stand, though, (f)scanf seems the clear winner.)

I feel strangely enlightened.
 
M

Mabden

Nick Keighley said:
"Mabden" <[email protected]> wrote in message

the scanf() solution won't have embedded spaces (as someone else pointed out
%s stops at the first whitespace). Whilst fgets() reads the whole line (or
as much of it as it has room for) including spaces. I was hinting that
fgets() isn't identical to scanf(). In a real program you might to check
it's a valid filename (control characters allowed?). It depends on your OS
what's allowed and what is sensible.

But, but, but ... doesn't everyone just use Windows...?

We love embedded spaces.

Spaces add... space.

Oh, I guess when you talk about a "real program" you mean something that
runs on VAX or DOS, or some other uppercase OS...
 
D

Dan Pop

In said:
the scanf() solution won't have embedded spaces (as someone else pointed out
%s stops at the first whitespace).

There's more to scanf than %s... Shall we also discard printf for
floating point values because %d expects an int value?

Dan
 
C

CBFalconer

Dan said:
(e-mail address removed) (Nick Keighley) writes:
.... snip ...

There's more to scanf than %s... Shall we also discard printf
for floating point values because %d expects an int value?

It would be nice. However we don't have a standardized set of
output routines, while we do have some suitable input routines in
strto**.

%s is what the newbie (to scanf) reaches for. Surprise. What is
needed is a standard set of i/o routines for primitive types
to/from text streams.

Eschew variadic functions.
 
B

Barry Schwarz

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

scanf ("%c", &filename);

reinterpret_cast <const char>( filename );


How did you declare filename?

char filename[1024];

or something similar? In this case, your scanf line should read

scanf("%s",filename);

because filename is already the address of the character array.
&filename points to the sky. Also, use %s !!!!! %c is for reading in a
character.

If filename is declared as an array as you suppose, &filename points
to the exact same address filename does. The problem is it would have
the wrong type. The %s format requires a pointer to char (which
filename is) while &filename would have pointer to array of char.
a=reinterpret_cast<char>(x);

is c++'s way of saying

a=(char) x;

for nonpolymorphic types.


Oh, there's no need! =) Just feed it a char. It will be implicitly
casted to const char.

The first parameter of fopen must be a pointer to char, not a char.


<<Remove the del for email>>
 
B

Barry Schwarz

from N869, conversion specifiers for fscanf:

s Matches a sequence of non-white-space
characters.228)

But the question was what is wrong with spaces in a filename, which
fgets will allow but NK seems to think need special attention.


<<Remove the del for email>>
 
C

CBFalconer

Barry said:
But the question was what is wrong with spaces in a filename, which
fgets will allow but NK seems to think need special attention.

The original code stopped input at the first trailing white space,
so the fgets call needs to be treated differently. As to what's
wrong with spaces in a filename, that is another large diatribe
which includes letting software firms without ethics control
standards.
 
N

Nick Keighley

There's more to scanf than %s... Shall we also discard printf for
floating point values because %d expects an int value?

I wasn't criticising %s format, just pointing the behaviour of scanf("%s")
is different from fgets(). Not better or worse, just different. I've no idea
if the OP wants to allow embedded spaces or not- I'm just saying he has to
allow for a change in behaviour.
 
D

Dan Pop

In said:
I wasn't criticising %s format, just pointing the behaviour of scanf("%s")
is different from fgets(). Not better or worse, just different. I've no idea
if the OP wants to allow embedded spaces or not- I'm just saying he has to
allow for a change in behaviour.

Who wrote: "avoid scanf()" above?

Dan
 
D

Dan Pop

In said:
It would be nice. However we don't have a standardized set of
output routines, while we do have some suitable input routines in
strto**.

Which of them is suitable for getting the name of an input file from the
user? The strtox routines are useful *only* after successfully obtaining
the input while this thread was focused on getting that input.

Have I ever recommended you to engage your brain *before* replying?

Dan
 
M

Mabden

Hey! you snipped my "What's wrong with embedded spaces?" comment! I was
getting a lot of mileage out of that one!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top