Connecting Tech Pros Worldwide Help | Site Map

converting variables

Nate
Guest
 
Posts: n/a
#1: Nov 14 '05
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?
Case
Guest
 
Posts: n/a
#2: Nov 14 '05

re: converting variables


Nate wrote:[color=blue]
> 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,[/color]

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

Case

Neil Cerutti
Guest
 
Posts: n/a
#3: Nov 14 '05

re: converting variables


In article <7ab023b6.0405180835.47fde09d@posting.google.com >,
Nate wrote:[color=blue]
> 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.[/color]

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
kal
Guest
 
Posts: n/a
#4: Nov 14 '05

re: converting variables


nate@nateharris.com (Nate) wrote in message news:<7ab023b6.0405180835.47fde09d@posting.google. com>...
[color=blue]
> printf("Enter the log file name: ");
>
> scanf ("%c", &filename);[/color]

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.
[color=blue]
> scanf ("%c", &filename);
>
> reinterpret_cast <const char>( filename );[/color]

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.
[color=blue]
> and get this error:
>
> OakleyParser.cpp(14) : error C2440: 'reinterpret_cast' : cannot
> convert from 'char' to 'char'[/color]

reinterpret_cast is meant to change the _data type_ and not qualifiers.
const is a data qualifier. Try looking up "const_cast".
[color=blue]
> So VS.Net recognizes both as Char, but to use the char variable
> ?filename' in fopen, I have to have a constant.[/color]

You should be able to pass a (char *) to a function expectng
(const char *) but not the other way around.
[color=blue]
> Any ideas?[/color]

Throw away the program and go FISHING!
Mitchell
Guest
 
Posts: n/a
#5: Nov 14 '05

re: converting variables


On 18 May 2004 09:35:22 -0700, nate@nateharris.com (Nate) wrote:
[color=blue]
> printf("Enter the log file name: ");
>
> scanf ("%c", &filename);
>
> reinterpret_cast <const char>( filename );[/color]


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.
[color=blue]
>OakleyParser.cpp(14) : error C2440: 'reinterpret_cast' : cannot
>convert from 'char' to 'char'[/color]

a=reinterpret_cast<char>(x);

is c++'s way of saying

a=(char) x;

for nonpolymorphic types.
[color=blue]
>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");[/color]

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

cheers!
Nick Keighley
Guest
 
Posts: n/a
#6: Nov 14 '05

re: converting variables


nate@nateharris.com (Nate) wrote in message news:<7ab023b6.0405180835.47fde09d@posting.google. com>...

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.
[color=blue]
> 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.[/color]

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).
[color=blue]
> 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.[/color]

it isn't necessary
[color=blue]
> This is what I tried, with no luck:
>
> printf("Enter the log file name: ");
> scanf ("%c", &filename);[/color]

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.
[color=blue]
> reinterpret_cast <const char>( filename );
>
> and get this error:
>
> OakleyParser.cpp(14) : error C2440: 'reinterpret_cast' : cannot
> convert from 'char' to 'char'[/color]

don't do this.
[color=blue]
> 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 *'[/color]

it's not the "const" that's the problem. Declare filename correctly
and all your problems go away.
[color=blue]
> Any ideas?[/color]

don't confuse char* with char


--
Nick Keighley
Mabden
Guest
 
Posts: n/a
#7: Nov 14 '05

re: converting variables


"Nick Keighley" <nick.keighley@marconi.com> wrote in message
news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...[color=blue]
> nate@nateharris.com (Nate) wrote in message[/color]
news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[color=blue]
>
>
> or better, avoid scanf() (its error recovery is poor) and use fgets()
> (you'll have to check for '\n' and embedded spaces.
>[/color]

What's wrong with embedded spaces?

--
Mabden


CBFalconer
Guest
 
Posts: n/a
#8: Nov 14 '05

re: converting variables


Mabden wrote:[color=blue]
> "Nick Keighley" <nick.keighley@marconi.com> wrote in message[color=green]
>> nate@nateharris.com (Nate) wrote in message
>>
>> or better, avoid scanf() (its error recovery is poor) and use
>> fgets() (you'll have to check for '\n' and embedded spaces.)[/color]
>
> What's wrong with embedded spaces?[/color]

from N869, conversion specifiers for fscanf:

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

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Nick Keighley
Guest
 
Posts: n/a
#9: Nov 14 '05

re: converting variables


"Mabden" <mabden@sbcglobal.net> wrote in message news:<qHNqc.1372$ik5.956@newssvr27.news.prodigy.co m>...[color=blue]
> "Nick Keighley" <nick.keighley@marconi.com> wrote in message
> news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...[color=green]
> > nate@nateharris.com (Nate) wrote in message[/color]
> news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[/color]
[color=blue][color=green]
> > or better, avoid scanf() (its error recovery is poor) and use fgets()
> > (you'll have to check for '\n' and embedded spaces.[/color]
>
> What's wrong with embedded spaces?[/color]

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.


--
Nick Keighley
Mabden
Guest
 
Posts: n/a
#10: Nov 14 '05

re: converting variables


"Nick Keighley" <nick.keighley@marconi.com> wrote in message
news:8ad2cfb3.0405192356.1ef83a04@posting.google.c om...[color=blue]
> "Mabden" <mabden@sbcglobal.net> wrote in message[/color]
news:<qHNqc.1372$ik5.956@newssvr27.news.prodigy.co m>...[color=blue][color=green]
> > "Nick Keighley" <nick.keighley@marconi.com> wrote in message
> > news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...[color=darkred]
> > > nate@nateharris.com (Nate) wrote in message[/color]
> > news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[/color]
>[color=green][color=darkred]
> > > or better, avoid scanf() (its error recovery is poor) and use fgets()
> > > (you'll have to check for '\n' and embedded spaces.[/color]
> >
> > What's wrong with embedded spaces?[/color]
>
> the scanf() solution won't have embedded spaces (as someone else pointed[/color]
out[color=blue]
> %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.[/color]

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

--
Mabden
I'm not a troll, but I play one on TV.


Sam Dennis
Guest
 
Posts: n/a
#11: Nov 14 '05

re: converting variables


Nick Keighley wrote:[color=blue]
> [for reading a line]
> scanf ("%s", filename);[/color]

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

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.

--
++acr@,ka"
Dan Pop
Guest
 
Posts: n/a
#12: Nov 14 '05

re: converting variables


In <8ad2cfb3.0405192356.1ef83a04@posting.google.com > nick.keighley@marconi.com (Nick Keighley) writes:
[color=blue]
>"Mabden" <mabden@sbcglobal.net> wrote in message news:<qHNqc.1372$ik5.956@newssvr27.news.prodigy.co m>...[color=green]
>> "Nick Keighley" <nick.keighley@marconi.com> wrote in message
>> news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...[color=darkred]
>> > nate@nateharris.com (Nate) wrote in message[/color]
>> news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[/color]
>[color=green][color=darkred]
>> > or better, avoid scanf() (its error recovery is poor) and use fgets()
>> > (you'll have to check for '\n' and embedded spaces.[/color]
>>
>> What's wrong with embedded spaces?[/color]
>
>the scanf() solution won't have embedded spaces (as someone else pointed out
>%s stops at the first whitespace).[/color]

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
CBFalconer
Guest
 
Posts: n/a
#13: Nov 14 '05

re: converting variables


Dan Pop wrote:[color=blue]
> nick.keighley@marconi.com (Nick Keighley) writes:
>[/color]
.... snip ...[color=blue][color=green]
>>
>> the scanf() solution won't have embedded spaces (as someone
>> else pointed out %s stops at the first whitespace).[/color]
>
> There's more to scanf than %s... Shall we also discard printf
> for floating point values because %d expects an int value?[/color]

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.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Barry Schwarz
Guest
 
Posts: n/a
#14: Nov 14 '05

re: converting variables


On Wed, 19 May 2004 10:37:57 +0800, Mitchell
<cheeHATESPAMliang@inaHATESPAMme.coHATESPAMm> wrote:
[color=blue]
>On 18 May 2004 09:35:22 -0700, nate@nateharris.com (Nate) wrote:
>[color=green]
>> printf("Enter the log file name: ");
>>
>> scanf ("%c", &filename);
>>
>> reinterpret_cast <const char>( filename );[/color]
>
>
>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.[/color]

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.
[color=blue]
>[color=green]
>>OakleyParser.cpp(14) : error C2440: 'reinterpret_cast' : cannot
>>convert from 'char' to 'char'[/color]
>
>a=reinterpret_cast<char>(x);
>
>is c++'s way of saying
>
>a=(char) x;
>
>for nonpolymorphic types.
>[color=green]
>>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");[/color]
>
>Oh, there's no need! =) Just feed it a char. It will be implicitly
>casted to const char.[/color]

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


<<Remove the del for email>>
Barry Schwarz
Guest
 
Posts: n/a
#15: Nov 14 '05

re: converting variables


On Wed, 19 May 2004 20:04:36 GMT, CBFalconer <cbfalconer@yahoo.com>
wrote:
[color=blue]
>Mabden wrote:[color=green]
>> "Nick Keighley" <nick.keighley@marconi.com> wrote in message[color=darkred]
>>> nate@nateharris.com (Nate) wrote in message
>>>
>>> or better, avoid scanf() (its error recovery is poor) and use
>>> fgets() (you'll have to check for '\n' and embedded spaces.)[/color]
>>
>> What's wrong with embedded spaces?[/color]
>
>from N869, conversion specifiers for fscanf:
>
> s Matches a sequence of non-white-space
> characters.228)[/color]

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>>
CBFalconer
Guest
 
Posts: n/a
#16: Nov 14 '05

re: converting variables


Barry Schwarz wrote:[color=blue]
> CBFalconer <cbfalconer@yahoo.com> wrote:[color=green]
>> Mabden wrote:[color=darkred]
>>> "Nick Keighley" <nick.keighley@marconi.com> wrote in message
>>>> nate@nateharris.com (Nate) wrote in message
>>>>
>>>> or better, avoid scanf() (its error recovery is poor) and use
>>>> fgets() (you'll have to check for '\n' and embedded spaces.)
>>>
>>> What's wrong with embedded spaces?[/color]
>>
>> from N869, conversion specifiers for fscanf:
>>
>> s Matches a sequence of non-white-space
>> characters.228)[/color]
>
> But the question was what is wrong with spaces in a filename, which
> fgets will allow but NK seems to think need special attention.[/color]

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.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

Nick Keighley
Guest
 
Posts: n/a
#17: Nov 14 '05

re: converting variables


Dan.Pop@cern.ch (Dan Pop) wrote in message news:<c8kpv3$34v$7@sunnews.cern.ch>...[color=blue]
> In <8ad2cfb3.0405192356.1ef83a04@posting.google.com > nick.keighley@marconi.com (Nick Keighley) writes:[color=green]
> >"Mabden" <mabden@sbcglobal.net> wrote in message news:<qHNqc.1372
> > $ik5.956@newssvr27.news.prodigy.com>...[color=darkred]
> >> "Nick Keighley" <nick.keighley@marconi.com> wrote in message
> >> news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...
> >> > nate@nateharris.com (Nate) wrote in message
> >> > news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >> > or better, avoid scanf() (its error recovery is poor) and use fgets()
> >> > (you'll have to check for '\n' and embedded spaces.
> >>
> >> What's wrong with embedded spaces?[/color]
> >
> >the scanf() solution won't have embedded spaces (as someone else pointed out
> >%s stops at the first whitespace).[/color]
>
> There's more to scanf than %s... Shall we also discard printf for
> floating point values because %d expects an int value?[/color]

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.


--
Nick Keighley
Dan Pop
Guest
 
Posts: n/a
#18: Nov 14 '05

re: converting variables


In <8ad2cfb3.0405240132.750843fa@posting.google.com > nick.keighley@marconi.com (Nick Keighley) writes:
[color=blue]
>Dan.Pop@cern.ch (Dan Pop) wrote in message news:<c8kpv3$34v$7@sunnews.cern.ch>...[color=green]
>> In <8ad2cfb3.0405192356.1ef83a04@posting.google.com > nick.keighley@marconi.com (Nick Keighley) writes:[color=darkred]
>> >"Mabden" <mabden@sbcglobal.net> wrote in message news:<qHNqc.1372
>> > $ik5.956@newssvr27.news.prodigy.com>...
>> >> "Nick Keighley" <nick.keighley@marconi.com> wrote in message
>> >> news:8ad2cfb3.0405190114.65ff66a1@posting.google.c om...
>> >> > nate@nateharris.com (Nate) wrote in message
>> >> > news:<7ab023b6.0405180835.47fde09d@posting.google. com>...[/color][/color]
>[color=green][color=darkred]
>> >> > or better, avoid scanf() (its error recovery is poor) and use fgets()[/color][/color][/color]
^^^^^^^^^^^^^[color=blue][color=green][color=darkred]
>> >> > (you'll have to check for '\n' and embedded spaces.
>> >>
>> >> 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).[/color]
>>
>> There's more to scanf than %s... Shall we also discard printf for
>> floating point values because %d expects an int value?[/color]
>
>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.[/color]

Who wrote: "avoid scanf()" above?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Dan Pop
Guest
 
Posts: n/a
#19: Nov 14 '05

re: converting variables


In <40AE0FD6.94518E19@yahoo.com> CBFalconer <cbfalconer@yahoo.com> writes:
[color=blue]
>Dan Pop wrote:[color=green]
>> nick.keighley@marconi.com (Nick Keighley) writes:
>>[/color]
>... snip ...[color=green][color=darkred]
>>>
>>> the scanf() solution won't have embedded spaces (as someone
>>> else pointed out %s stops at the first whitespace).[/color]
>>
>> There's more to scanf than %s... Shall we also discard printf
>> for floating point values because %d expects an int value?[/color]
>
>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**.[/color]

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Mabden
Guest
 
Posts: n/a
#20: Nov 14 '05

re: converting variables


"Dan Pop" <Dan.Pop@cern.ch> wrote in message
news:c8srlb$c4s$4@sunnews.cern.ch...[color=blue]
> In <40AE0FD6.94518E19@yahoo.com> CBFalconer <cbfalconer@yahoo.com> writes:
>[color=green]
> >Dan Pop wrote:[color=darkred]
> >> nick.keighley@marconi.com (Nick Keighley) writes:
> >>[/color]
> >... snip ...[/color][/color]

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

--
Mabden


Dan Pop
Guest
 
Posts: n/a
#21: Nov 14 '05

re: converting variables


In <8PAsc.72682$f66.64145@newssvr25.news.prodigy.co m> "Mabden" <mabden@sbcglobal.net> writes:
[color=blue]
>"Dan Pop" <Dan.Pop@cern.ch> wrote in message
>news:c8srlb$c4s$4@sunnews.cern.ch...[color=green]
>> In <40AE0FD6.94518E19@yahoo.com> CBFalconer <cbfalconer@yahoo.com> writes:
>>[color=darkred]
>> >Dan Pop wrote:
>> >> nick.keighley@marconi.com (Nick Keighley) writes:
>> >>
>> >... snip ...[/color][/color]
>
>Hey! you snipped my "What's wrong with embedded spaces?" comment! I was
>getting a lot of mileage out of that one![/color]

If *properly* used, scanf has no problem handling embedded spaces. It's
just that %s is not the right conversion specification in this case.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Closed Thread


Similar C / C++ bytes