473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting variables

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_cas t 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_cas t <const char>( filename );

and get this error:

OakleyParser.cp p(14) : error C2440: 'reinterpret_ca st' : 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.cp p(20) : error C2664: 'fopen' : cannot convert parameter
1 from 'char' to 'const char *'

Any ideas?
Nov 14 '05 #1
20 2189
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_cas t 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

Nov 14 '05 #2
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_cas t 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
Nov 14 '05 #3
kal
na**@nateharris .com (Nate) wrote in message news:<7a******* *************** ****@posting.go ogle.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_cas t <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.cp p(14) : error C2440: 'reinterpret_ca st' : cannot
convert from 'char' to 'char'
reinterpret_cas t 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!
Nov 14 '05 #4
On 18 May 2004 09:35:22 -0700, na**@nateharris .com (Nate) wrote:
printf("Enter the log file name: ");

scanf ("%c", &filename);

reinterpret_cas t <const char>( filename );

How did you declare filename?

char filename[1024];

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

scanf("%s",file name);

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.c pp(14) : error C2440: 'reinterpret_ca st' : cannot
convert from 'char' to 'char'
a=reinterpret_c ast<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!
Nov 14 '05 #5
na**@nateharris .com (Nate) wrote in message news:<7a******* *************** ****@posting.go ogle.com>...

first decide which language you are programming in C or C++. C has no
reinterpret_cas t 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_cas t 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_cas t <const char>( filename );

and get this error:

OakleyParser.cp p(14) : error C2440: 'reinterpret_ca st' : 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.cp p(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
--
Nick Keighley
Nov 14 '05 #6
"Nick Keighley" <ni***********@ marconi.com> wrote in message
news:8a******** *************** ***@posting.goo gle.com...
na**@nateharris .com (Nate) wrote in message news:<7a******* *************** ****@posting.go ogle.com>...

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?

--
Mabden
Nov 14 '05 #7
Mabden wrote:
"Nick Keighley" <ni***********@ marconi.com> wrote in message
na**@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?


from N869, conversion specifiers for fscanf:

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

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #8
"Mabden" <ma****@sbcglob al.net> wrote in message news:<qH******* *********@newss vr27.news.prodi gy.com>...
"Nick Keighley" <ni***********@ marconi.com> wrote in message
news:8a******** *************** ***@posting.goo gle.com...
na**@nateharris .com (Nate) wrote in message

news:<7a******* *************** ****@posting.go ogle.com>...

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?


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
Nov 14 '05 #9
"Nick Keighley" <ni***********@ marconi.com> wrote in message
news:8a******** *************** ***@posting.goo gle.com...
"Mabden" <ma****@sbcglob al.net> wrote in message news:<qH******* *********@newss vr27.news.prodi gy.com>...
"Nick Keighley" <ni***********@ marconi.com> wrote in message
news:8a******** *************** ***@posting.goo gle.com...
na**@nateharris .com (Nate) wrote in message

news:<7a******* *************** ****@posting.go ogle.com>...

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?


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

--
Mabden
I'm not a troll, but I play one on TV.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6896
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by using fprintf. The problem is that I don't know how to convert a field of 6 octets? Should I use a long variable? Thanks
12
3196
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
4
3196
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can get public members but not protected, private, static members"
12
2612
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for example: 21/05/2006
5
1405
by: stephen | last post by:
Hi, I have been working using VB .NET and I wanted to convert a code to C# in VB .Net I use modules so that I can declare variables, STP holders so that I can replace them easily when I move b/w Dev and Prod like this Module modAVSVariables 'Arraylist and Array Variables Friend pathArray As Array Friend temp_arrAllFiles As Array Friend getPath As ArrayList = New ArrayList
1
1790
by: coolindienc | last post by:
I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where I think the problem occured after my changes. Old Code working FINE from math import * def menu(): global menuSel print "\nSlect one of the following:" print "1. Calculate Area of Rectangle"
0
2124
by: rpjd | last post by:
Apache2 over XP Home, PHP5, PostgreSQL8.2 If this is not the correct forum for this, it can be reposted accordingly. I have php variables/arrays that I want to display in a php webpage. What I am doing is using my server-side php script to generate javascript and convert my php data. The 1st bit of code is my php script, the 2nd is my javascript from my php webpage. I have 2 variables $numrows and $numfields, and 2 arrays $fieldname and...
1
6643
by: dishal | last post by:
Can anyone help me please? How do I convert these codes to launch from a JFrame instead of a Java Applet? A simple program where the user can sketch curves and shapes in a variety of colors on a variety of background colors. The user selects a drawing color form a pop-up menu at the top of the applet. If the user clicks "Set Background", the background color is set to the current drawing color and the drawing ...
12
2126
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef unsigned char byte; // 'byte' is an 8-bit unsigned value
3
2503
by: sparks | last post by:
After changing all the variables in a table from long to double. you get 9.1====becomes==== 9.19999980926514 ok I checked microsoft and they had a workaround. export the values to excel...then import them back into the new table. well I tried that and get the same thing. is there a work around for the work around LOL
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.