By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

Command line character problem

P: n/a
mdh
Hi all,
I have a file, whose path is:

"/Users/m/k&R/test_file"
How do I include the '&' in a string constant? ( I need this for the
example on p162). I have tried to use the Hex notation x26, as in

"/Users/m/k\x26R/test_file".

On it's own, an ampersand in the literal causes an error. Using the
escape sequence, I get no error, but neither do I get the result I
want! :-)

Could anyone give some guidance.

Thank you as usual.

Sep 29 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi

On Mon, 29 Sep 2008 05:21:35 -0700, mdh wrote:
"/Users/m/k&R/test_file"

How do I include the '&' in a string constant? ( I need this for the
example on p162). I have tried to use the Hex notation x26, as in

"/Users/m/k\x26R/test_file".

On it's own, an ampersand in the literal causes an error. Using the
escape sequence, I get no error, but neither do I get the result I want!
There is nothing special about the ampersand in a C string literal, you
have got something else wrong.

Post the exact code you tried to compile and the exact message you
received.

Sep 29 '08 #2

P: n/a
mdh said:
Hi all,
I have a file, whose path is:

"/Users/m/k&R/test_file"
How do I include the '&' in a string constant?
The answer to your question is right there in the question itself.

"/Users/m/k&R/test_file"
On it's own, an ampersand in the literal causes an error.
This is nothing to do with C, and everything to do with your filesystem.

It may be worth trying this:

"/Users/m/k\\&R/test_file"

but really this is a shell question, not a C question.

If all you want is a quick fix, why not rename the directory to
/Users/m/kandr/test_file ? Just a thought.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 29 '08 #3

P: n/a
On Sep 29, 3:21 pm, mdh <m...@comcast.netwrote:
Hi all,
I have a file, whose path is:

"/Users/m/k&R/test_file"

How do I include the '&' in a string constant? ( I need this for the
example on p162). I have tried to use the Hex notation x26, as in

"/Users/m/k\x26R/test_file".

On it's own, an ampersand in the literal causes an error. Using the
escape sequence, I get no error, but neither do I get the result I
want! :-)
The error you get is not related to the ampersand.
C guarantees that '&' 0, but not that '&' == 0x26.
(not all systems have ASCII)

What was the original problem that led you to believe the ampresand
was the problem?
Sep 29 '08 #4

P: n/a
mdh
On Sep 29, 5:36*am, Richard Heathfield <r...@see.sig.invalidwrote:
>

This is nothing to do with C, and everything to do with your filesystem.

It may be worth trying this:

"/Users/m/k\\&R/test_file"

but really this is a shell question, not a C question.
Hi Richard...yes...I was afraid that it would be this. Thanks.
Sep 29 '08 #5

P: n/a
mdh wrote:
Hi all,
I have a file, whose path is:

"/Users/m/k&R/test_file"
How do I include the '&' in a string constant? ( I need this for the
example on p162). I have tried to use the Hex notation x26, as in

"/Users/m/k\x26R/test_file".

On it's own, an ampersand in the literal causes an error. Using the
escape sequence, I get no error, but neither do I get the result I
want! :-)
Since there's nothing in any way special about the ampersand
in a "string constant" or "literal" appearing in C source code,
I suspect you're talking about something else entirely.

My guess, from your Subject line, is that you're having
trouble providing the file name to the program as a command-line
argument. Perhaps the "command interpreter" through which you
launch programs attaches special meaning to the ampersand -- many
Unix "shells" do. If that's the problem, your question isn't
about C at all, but about how to use the command line on your
system (whatever it is). Try a forum that discusses your system.

<off-topic>

If you're using a Unix system,

programname /Users/m/k\&r/test_file

should work with most shells.

</off-topic>

When you have questions in the future, please try to give
a fuller description of your problem. You say that something
you tried gave "an error," but you coyly conceal the nature of
that error ... Don't rely on us (and certainly not on me!) to
be able to intuit all the details you omit; our crystal balls
are either cloudy or foreclosed on, and our intuition may lead
us to diagnose some problem entirely unlike the one that afflicts
you. If so, the time you've wasted reading my non-answer is
your own fault, and your own punishment. Harrrumph!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 29 '08 #6

P: n/a
mdh
On Sep 29, 5:36*am, vipps...@gmail.com wrote:
On Sep 29, 3:21 pm, mdh <m...@comcast.netwrote:
Hi all,
I have a file, whose path is:
"/Users/m/k&R/test_file"
>
The error you get is not related to the ampersand.
C guarantees that '&' 0, but not that '&' == 0x26.
(not all systems have ASCII)


Thanks for your input.
Sep 29 '08 #7

P: n/a
mdh
On Sep 29, 5:41*am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>

</off-topic>

* * *When you have questions in the future, please try to give
a fuller description of your problem. *You say that something
you tried gave "an error," but you coyly conceal the nature of
that error ... *Don't rely on us (and certainly not on me!) to
be able to intuit all the details you omit; our crystal balls
are either cloudy or foreclosed on, and our intuition may lead
us to diagnose some problem entirely unlike the one that afflicts
you. *If so, the time you've wasted reading my non-answer is
your own fault, and your own punishment. *Harrrumph!

Point taken...you are the last person I wish to alienate.

Sep 29 '08 #8

P: n/a
mdh <md**@comcast.netwrites:
I have a file, whose path is:

"/Users/m/k&R/test_file"
How do I include the '&' in a string constant? ( I need this for the
example on p162).
Precisely as you've done above.
I have tried to use the Hex notation x26, as in

"/Users/m/k\x26R/test_file".
That will give you a character with the value 0x26, which the standard
does not guarntee to be '&'. If it is (as it almost certainly is on
your system), "...\x26..." means *exactly* the same thing as
"...&...", so you can't expect it to solve your problem. If it isn't,
then it obviously won't solve your problem.
On it's own, an ampersand in the literal causes an error.
No, it doesn't, at least not directly. An ampersand is a perfectly
legal character in a string literal. An ampersand in the resulting
string value may indirectly cause some other error, but you haven't
given us a clue what that error might be.
Using the
escape sequence, I get no error, but neither do I get the result I
want! :-)
How are we supposed to know what result you want?
Could anyone give some guidance.
Yes, ask better questions.

I can guess with some confidence what your problem is. I've written
up a fairly detailed explanation, but I won't post it here, since it's
system-specific; if your e-mail address is valid, I'll send it to you
directly. But if you want a useful answer, you'll have to provide
more information. Usually the best thing is to post a small,
complete, compilable program that illustrates your problem, not just a
single string literal removed from any context. (Hint: I'll bet
you're passing it to system().)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 29 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.