String Character Conversion

J

Jayson Davis

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?
 
E

Eric Sosman

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

Jayson Davis

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



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

Jack Klein

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

Eric Sosman

Jayson Davis wrote On 06/30/06 12:43,:
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.
 
J

Jayson Davis

Eric said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top