Converting char array of literals chars to escape chars 
Author Message
 Converting char array of literals chars to escape chars

I can't seem to think of a quick solution to this, any ideas are
appreciated.

Here the problem: I have an array of chars that was fgets() from a
user supplied file

lineterms[MAX]

as an example, lineterms[] may have the following values:

lineterms[0] = '\'
lineterms[1] = 'n'
lineterms[2] = '\'
lineterms[3] = 'r'
lineterms[4] = '\n'
lineterms[5] = '\0'
lineterms[6] = '\0' and the rest are all NULL too

now i can walk along the array and replace the 5th element '\n' with a
NULL character, so now my array represented as a string looks like
this: "\n\r". These are 4 literal characters, namely the backslash,
the 'n', another backslash, and an 'r'. However, I want to have this
represented as just two characters, the newline (ASCII 10), and the
carriage return (ASCII 8). in other words I want it to be treated how
it is in a printf("\n\f");

is there a simple way to achieve this?



Thu, 04 Mar 2004 04:35:40 GMT  
 Converting char array of literals chars to escape chars

Quote:

>I can't seem to think of a quick solution to this, any ideas are
>appreciated.

>Here the problem: I have an array of chars that was fgets() from a
>user supplied file

>lineterms[MAX]

>as an example, lineterms[] may have the following values:

>lineterms[0] = '\'
>lineterms[1] = 'n'
>lineterms[2] = '\'
>lineterms[3] = 'r'
>lineterms[4] = '\n'
>lineterms[5] = '\0'
>lineterms[6] = '\0' and the rest are all NULL too

Okay, so in other words the file contained the text "\\n\\r\n"

Quote:
>now i can walk along the array and replace the 5th element '\n' with a
>NULL character, so now my array represented as a string looks like
>this: "\n\r". These are 4 literal characters, namely the backslash,
>the 'n', another backslash, and an 'r'. However, I want to have this
>represented as just two characters, the newline (ASCII 10), and the

ASCII 10 is known as ``linefeed''. ``Newline'' is an abstract C concept
connecting the character '\n' (also called ``newline'') with its semantic
action of terminating lines in text streams, and its intended display
semantics of advancing the position to the next line.

Quote:
>carriage return (ASCII 8). in other words I want it to be treated how

Note that 8 is backspace; carriage return is 13.

Quote:
>it is in a printf("\n\f");

>is there a simple way to achieve this?

The C language does not give the programmer access to the language
translator. And in fact C programs may have an execution environment
that is separate from the translation environment.  A few standard
functions are provided for tokenizing and scanning numeric constants,
but no library function is provided to scan text which represents string
literals and turn it into a string object, as a C compiler would do
it. You must write the function yourself; step through the source string
character by character, and process the backslash escape sequences,
writing the translation to a destination string.


Thu, 04 Mar 2004 04:48:05 GMT  
 Converting char array of literals chars to escape chars


Quote:
>I can't seem to think of a quick solution to this, any ideas are
>appreciated.

>Here the problem: I have an array of chars that was fgets() from a
>user supplied file

>lineterms[MAX]

>as an example, lineterms[] may have the following values:

>lineterms[0] = '\'
>lineterms[1] = 'n'
>lineterms[2] = '\'
>lineterms[3] = 'r'
>lineterms[4] = '\n'
>lineterms[5] = '\0'
>lineterms[6] = '\0' and the rest are all NULL too

>now i can walk along the array and replace the 5th element '\n' with a
>NULL character, so now my array represented as a string looks like
>this: "\n\r". These are 4 literal characters, namely the backslash,
>the 'n', another backslash, and an 'r'. However, I want to have this
>represented as just two characters, the newline (ASCII 10), and the
>carriage return (ASCII 8). in other words I want it to be treated how
>it is in a printf("\n\f");

>is there a simple way to achieve this?

If you are sure the '\n' is the last character before the '\0', you
could use
        lineterms[strlen(lineterms)] = '\0';

If you are not sure but want to replace the first or last '\n', you
could use strchr() or strrchr() to find it, respectively.

Neither ASCII 8 nor \f represent the carriage return.  It is
represented by \r and in ASCII is 13 or 0x0d.

Once you sort out what your substitutions are, you can use strstr() to
find the occurrence of "\\n\\r" (the four character sequence you are
interested in) in your string.  Then use strncpy() to replace the
first two with whatever you decided (e.g., "\n\r").  Finally use
memmove() to move the remaining characters (including the terminating
'\0') up two bytes to eliminate the remaining bytes of the sequence
that were not replace with the substitution.  (memmove() is the only
one that is guaranteed to work even if the source and destination
areas overlap.)

Repeat the above if the sequence can appear more than once in the
string.

<<Remove the del for email>>



Thu, 04 Mar 2004 05:13:28 GMT  
 Converting char array of literals chars to escape chars

Quote:

> NULL character,

null character, is what you mean.
NULL, is a zero value for pointers.

--
 pete



Sat, 06 Mar 2004 19:02:57 GMT  
 Converting char array of literals chars to escape chars

[ input line containing \\ n \\ r \n ]
...
Quote:
> If you are sure the '\n' is the last character before the '\0', you
> could use
> lineterms[strlen(lineterms)] = '\0';

-1 of course.  And even if you think you're sure, at least check
lineterms[0] != '\0' (hence strlen(lineterms) > 0) in case you're wrong.

Quote:
> If you are not sure but want to replace the first or last '\n', you
> could use strchr() or strrchr() to find it, respectively.

And check if the result is a null pointer before you dereference it.

Quote:
> Once you sort out what your substitutions are, you can use strstr() to
> find the occurrence of "\\n\\r" ... Then use strncpy() ... memmove() ...

If you are interested only in that _sequence_, which is not how
I (and other responders) interpret the OP's case.

--
- David.Thompson 1 now at worldnet.att.net



Fri, 12 Mar 2004 12:12:26 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. A char pointer (char *) vs. char array question

2. Differences between char array[SIZE] and char *array

3. char *c,char c[ ] and char *c[ ]

4. char, unsigned char, signed char

5. Convert char* to char __gc[]

6. cannot convert from char* to char[4]

7. Convert four chars to a long and a long to four chars

8. Converting unsigned char to signed char invokes UB?

9. converting char to pointer char

10. Convert an 8bit char in a 7bit char!

11. converting const char* to unsigned char[1024]

12. Convert from char to unsigned char

 

 
Powered by phpBB® Forum Software