Newline in commandline string 
Author Message
 Newline in commandline string

Good Morning (At least it's morning in Sweden now)

I encountered the followinig problem when I'm trying to make use of a string
in my C-program, that is supplied as an argument on the commandline.

To simplify my program it may look like this:

main(unsigned int argc, char *argv[])
{
   printf("%s", argv[1]);

Quote:
}

If we name the program 'prstr' I would run it like this:

Quote:
> prstr AB

This will print AB on the screen, which is perfectly OK. However if I
want a newline to be printed as well I presumed I could write:

Quote:
> prstr "A\nB"

But this gives me A\nB on the screen. I've debugged the program and
the string is acctually three characters long.
argv[1][0] = 'A'
argv[1][1] = '\n'
argv[1][2] = 'B'

I've also tried running the program with the following stringformat:

Quote:
> prstr A\nB
> prstr 'A\nB'

Why don't I get a newline in my printout ??

My OS is Ultrix.

Tom
--
      +------------------------+------------------------+

      | IKEA of Sweden         | Phone: +46(0)476-82433 |
      | Box 702                | Fax:   +46(0)476-12717 |
      | 383 81 Almhult, Sweden |                        |
      +------------------------+------------------------+



Sun, 04 Feb 1996 15:11:53 GMT  
 Newline in commandline string

Quote:
>To simplify my program it may look like this:

>main(unsigned int argc, char *argv[])
>{
>   printf("%s", argv[1]);
>}

>If we name the program 'prstr' I would run it like this:

>> prstr AB

>This will print AB on the screen, which is perfectly OK. However if I
>want a newline to be printed as well I presumed I could write:

>> prstr "A\nB"

Nobody is going to translate this \n to a newline. This translation is
performed at compile time, but at run time the string is left as it is.
Quote:

>But this gives me A\nB on the screen. I've debugged the program and
>the string is acctually three characters long.
>argv[1][0] = 'A'
>argv[1][1] = '\n'

               ^^
I can't believe that. These are two different characters, each occupying
a different position in the string. To support my affirmation, I've made a
slight change to the program:

dxcern:~ 44> uname -a
ULTRIX dxcern.cern.ch 4.3 0 RISC
dxcern:~ 45> cat prstr.c
main(unsigned int argc, char *argv[])
{
  while (*argv[1])printf("%c\n",*argv[1]++);

Quote:
}

dxcern:~ 46> ./prstr "A\nB"
A
\
n
B

Quote:

>Why don't I get a newline in my printout ??

Now, you have the answer.

Quote:
>My OS is Ultrix.

Dan
--
Dan Pop
Tel:   +41.22.767.2335

Mail:  CERN - PPE, Bat. 21 1-023, CH-1211 Geneve 23, Switzerland


Sun, 04 Feb 1996 18:48:25 GMT  
 Newline in commandline string

Quote:


>>To simplify my program it may look like this:

>>main(unsigned int argc, char *argv[])
>>{
>>   printf("%s", argv[1]);
>>}

>>If we name the program 'prstr' I would run it like this:

>>> prstr AB

>>This will print AB on the screen, which is perfectly OK. However if I
>>want a newline to be printed as well I presumed I could write:

>>> prstr "A\nB"

> Nobody is going to translate this \n to a newline. This translation is
> performed at compile time, but at run time the string is left as it is.

>>But this gives me A\nB on the screen. I've debugged the program and
>>the string is acctually three characters long.
>>argv[1][0] = 'A'
>>argv[1][1] = '\n'
>                ^^
> I can't believe that. These are two different characters, each occupying
> a different position in the string. To support my affirmation, I've made a
> slight change to the program:

> dxcern:~ 44> uname -a
> ULTRIX dxcern.cern.ch 4.3 0 RISC
> dxcern:~ 45> cat prstr.c
> main(unsigned int argc, char *argv[])
> {
>   while (*argv[1])printf("%c\n",*argv[1]++);
> }

> dxcern:~ 46> ./prstr "A\nB"
> A
> \
> n
> B

>>Why don't I get a newline in my printout ??

> Now, you have the answer.

>>My OS is Ultrix.

> Dan
> --

Well, we're sort of both right. If I run my program IN the de{*filter*} (dxdb)
I get my result the string IS three characters long 'A', '\n' and 'B'. But
if I don't run it through the de{*filter*} the string is four characters 'A',
'\', 'n', 'B'.  ?????????????

Anyway, what do I write on the commandline to get a newline character
into argv[1] ?

--
      +------------------------+------------------------+

      | IKEA of Sweden         | Phone: +46(0)476-82433 |
      | Box 702                | Fax:   +46(0)476-12717 |
      | 383 81 Almhult, Sweden |                        |
      +------------------------+------------------------+



Sun, 04 Feb 1996 20:13:31 GMT  
 Newline in commandline string

Quote:
>Anyway, what do I write on the commandline to get a newline character
>into argv[1] ?

prstr "A\
B"

After the '\', press Return and continue on the next line. Note that this is
a feature of the shell and has nothing to do with C.

Dan
--
Dan Pop
Tel:   +41.22.767.2335

Mail:  CERN - PPE, Bat. 21 1-023, CH-1211 Geneve 23, Switzerland



Sun, 04 Feb 1996 21:33:21 GMT  
 Newline in commandline string

Quote:


>>Anyway, what do I write on the commandline to get a newline character
>>into argv[1] ?

>prstr "A\
>B"

>After the '\', press Return and continue on the next line. Note that this is
>a feature of the shell and has nothing to do with C.

For the record, this is correct for csh, but not for sh; sh allows
newlines in quoted strings without any need for a backslash.  (It turns
out that the backslash causes the newline to be ignored in sh, in fact.)
--
Eric Amick
S3 Technologies                 I'm not paid enough to have opinions!  ;-)
Columbia, MD


Tue, 06 Feb 1996 04:55:04 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do you get a newline in a textbox and string in C#

2. insert newlines into long string

3. string newline replace

4. length of strings w. newlines in them

5. stripping newlines from strings

6. newlines in string constants

7. building a string with newline characters

8. newline in string

9. Adding a newline to a string

10. formatting long strings using sprintf() without including tabs or newlines

11. Remove newlines from string

12. passing commandline parameters HELP???

 

 
Powered by phpBB® Forum Software