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

String Character Conversion

P: n/a


Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Jun 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Jayson Davis wrote On 06/30/06 11:46,:

Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?


Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.

--
Er*********@sun.com

Jun 30 '06 #2

P: n/a
Eric Sosman wrote:

Jayson Davis wrote On 06/30/06 11:46,:
Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.


Thanks for the reply. I'm taking a summer course in C and this is all a
tad overwhelming having it shoved into such a short timespan.

The string as read is "Needle\n\n", but what the string really needs to
be is "Needle\x0A0\x0A" in order for strstr() or strcmp() to work. In
other words, the string is literal "\n" and not '\n'.

I've gone through the string.h functions and did Google searches for
functions that would make replacement a little easier, but it would
appear I'm going to have to construct something myself. I'm curious if
there's a slick way to do it, or just trudge through one character at a
time.
Jun 30 '06 #3

P: n/a
On Fri, 30 Jun 2006 15:46:10 GMT, Jayson Davis
<ec************@yahoo.com> wrote in comp.lang.c:


Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?


Your question is not clear. What do you mean by "it"? What "it"
hasn't converted the linefeeds into characters? Everything you can
ever read from any file is composed of characters.

Do you mean that, on a single line read from a text file, you
literally have the four characters '\', 'n', '\', 'n' as the last four
text characters before the end of the line?

If that's what you mean, you will need to interpret the meaning in
your program. The mapping of escape sequences like \n, \t, \b, and so
on, in string literals and character constants, to single characters
is a feature of the compiler and performed when translating the source
code at compile time. There is nothing in the standard library that
does this translation for you at run time.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 30 '06 #4

P: n/a


Jayson Davis wrote On 06/30/06 12:43,:
Eric Sosman wrote:
Jayson Davis wrote On 06/30/06 11:46,:

Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.

Thanks for the reply. I'm taking a summer course in C and this is all a
tad overwhelming having it shoved into such a short timespan.

The string as read is "Needle\n\n", but what the string really needs to
be is "Needle\x0A0\x0A" in order for strstr() or strcmp() to work. In
other words, the string is literal "\n" and not '\n'.


Aha! I think I may have (*may* have) figured out
your difficulty. When you say the string as read is
"Needle\n\n", do you mean that there are ten characters
before the zero, the last four of which are "backslash,
n, backslash, n?" And that what you want is an eight-
character string ending in "newline, newline?"

If so, then no: There is nothing in the Standard
library that duplicates the operation of the C compiler
in translating escape sequences to characters. Those
escape sequences are a convention used only (as far as
C itself is concerned) in C source code, and the special
handling given to them by the compiler is not part of
the run-time library.

Your program, it seems, wants to use a similar
convention to represent "difficult" characters. If
that's what you're after, you'll need to write your
own translation code. Depending on how many of the C
source forms you decide to accept, the size of this
project could be anywhere from trivial to smallish.

--
Er*********@sun.com

Jun 30 '06 #5

P: n/a
Eric Sosman wrote:
Aha! I think I may have (*may* have) figured out
your difficulty. When you say the string as read is
"Needle\n\n", do you mean that there are ten characters
before the zero, the last four of which are "backslash,
n, backslash, n?" And that what you want is an eight-
character string ending in "newline, newline?"
That is exactly my difficulty. The string is ten characters and in
order to do the string comparison, I need it ending in "newline newline".
Your program, it seems, wants to use a similar
convention to represent "difficult" characters. If
that's what you're after, you'll need to write your
own translation code. Depending on how many of the C
source forms you decide to accept, the size of this
project could be anywhere from trivial to smallish.


That's what I was fearing. Oh well, maybe this lil' gotcha is why it is
part of the homework.

Thanks for your assistance and to the other folks who replied.
Jun 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.