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