473,803 Members | 3,637 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
20 2191
Nick Keighley wrote:
[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.

--
++acr@,ka"
Nov 14 '05 #11
In <8a************ **************@ posting.google. com> ni***********@m arconi.com (Nick Keighley) writes:
"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).


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: Da*****@ifh.de
Nov 14 '05 #12
Dan Pop wrote:
ni***********@m arconi.com (Nick Keighley) writes:

.... snip ...

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?


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 (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 #13
On Wed, 19 May 2004 10:37:57 +0800, Mitchell
<ch************ ***@inaHATESPAM me.coHATESPAMm> wrote:
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",fil ename);

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.
OakleyParser. cpp(14) : error C2440: 'reinterpret_ca st' : 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.


The first parameter of fopen must be a pointer to char, not a char.
<<Remove the del for email>>
Nov 14 '05 #14
On Wed, 19 May 2004 20:04:36 GMT, CBFalconer <cb********@yah oo.com>
wrote:
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)


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>>
Nov 14 '05 #15
Barry Schwarz wrote:
CBFalconer <cb********@yah oo.com> wrote:
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)


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.

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

Nov 14 '05 #16
Da*****@cern.ch (Dan Pop) wrote in message news:<c8******* ***@sunnews.cer n.ch>...
In <8a************ **************@ posting.google. com> ni***********@m arconi.com (Nick Keighley) writes:
"Mabden" <ma****@sbcglob al.net> wrote in message news:<qHNqc.137 2
$i******@newssv r27.news.prodig y.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).


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.
--
Nick Keighley
Nov 14 '05 #17
In <8a************ **************@ posting.google. com> ni***********@m arconi.com (Nick Keighley) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<c8******* ***@sunnews.cer n.ch>...
In <8a************ **************@ posting.google. com> ni***********@m arconi.com (Nick Keighley) writes:
>"Mabden" <ma****@sbcglob al.net> wrote in message news:<qHNqc.137 2
> $i******@newssv r27.news.prodig y.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).


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.


Who wrote: "avoid scanf()" above?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #18
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
ni***********@m arconi.com (Nick Keighley) writes:

... snip ...

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?


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #19
"Dan Pop" <Da*****@cern.c h> wrote in message
news:c8******** **@sunnews.cern .ch...
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
ni***********@m arconi.com (Nick Keighley) writes:

... snip ...


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

--
Mabden
Nov 14 '05 #20

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

Similar topics

3
6897
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
3200
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
3197
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
2613
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
1406
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
1792
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
2125
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
6645
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
2136
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
2505
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
9703
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
9564
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
10548
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
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
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
7604
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
6842
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();...

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.