Operating System - HP-UX
1753682 Members
5345 Online
108799 Solutions
New Discussion юеВ

Re: sprintf does not preserve quotes in c program

 
Dave Chamberlin
Trusted Contributor

sprintf does not preserve quotes in c program

Greetings - C programming issue
I want to program to output double quotes - this works:
printf(" \"hello\" \n"); to output "hello"
this SHOULD work but does not:
sprintf(s,"\"hello\" \n");
system(s) --output is just hello without the double quotes. Why? thanks...
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: sprintf does not preserve quotes in c program

Hi Dave:

This works for me:

#include
int main() {
char s[20];
sprintf( s, "\"hello\" \n");
printf("%s", s);
}

...using B3901BA B.11.11.16 HP C/ANSI C Developer's Bundle for HP-UX (S800)

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: sprintf does not preserve quotes in c program

> system(s)

Uh, what do you think this does?

> this SHOULD work but does not:

Depends on what you mean by "work". If you
mean, "attempt to run the command, '"hello"',
then it works. Lacking a complete example
program, I made one up.

alp $ type sp.c
#include
#include

main()
{
char s[133];

sprintf(s,"\"hello\" \n");
system(s);
}
alp $ cc sp
alp $ link sp
alp $ run sp
%DCL-W-IVVERB, unrecognized command verb - check validity and spelling
\"hello"\

The quotation marks are there.

Or, on an HP-UX system;

dy # cat sp.c
#include
#include

main()
{
char s[133];

sprintf(s,"\"hello\" \n");
system(s);
}
dy # cc -o sp sp.c
dy # ./sp
sh: hello: not found.

Much like a simple shell command:

dy # sh "hello"
sh: hello: not found.

The shell eats the quotation marks.

What, exactly, are you trying to do (or
expecting to happen)?
Dave Chamberlin
Trusted Contributor

Re: sprintf does not preserve quotes in c program

I am trying to feed a command to the shell with quotes - like: echo "testing" > testfile...

thanks
James R. Ferguson
Acclaimed Contributor

Re: sprintf does not preserve quotes in c program

Hi (again) Dave:

Oh, if your are *actually* calling system() to pass the string you constructed, as shown in Steven's example [which I missed in your post because it didn't seem to make sense], then this example seems contrived.

Substitute "date" for "hello" and the system() will return today's date. The idea is that you are *executing* the command string using the Posix shell. Your code isn't any different then:

# sh -c "hello"

See the manpages for 'system(3S)'.

Regards!

...JRF...



Steven Schweda
Honored Contributor

Re: sprintf does not preserve quotes in c program

> I am trying to [...]

Perhaps an actual failing test program would
be easier to diagnose than waving hands.

First, you should probably lose the "\n".

My HP-UX system is off again, but:

alp $ type sp3.c
#include
#include

main()
{
char s[133];

sprintf(s,"pipe write sys$output \"hel lo\" > sp3.out");
system(s);
}
alp $ cc sp3
alp $ link sp3
alp $ run sp3
alp $ type sp3.out
hel lo
alp $

I'd say that the quotation is working.
James R. Ferguson
Acclaimed Contributor

Re: sprintf does not preserve quotes in c program

Hi (again) Dave:

#include
int main() {
char s[20];
sprintf( s, "\134" "\042" "hello" "\134" "\042" "\n" );
system(s);
}

...when compiled and run:

sh: "hello": not found.

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: sprintf does not preserve quotes in c program

dyi # cat sp3u.c
#include
#include

main()
{
char s[133];

sprintf(s,"echo \"hel lo\" > sp3u.out");
system(s);
}
dyi # cc -o sp3u sp3u.c
dyi # ./sp3u
dyi # cat sp3u.out
hel lo
dyi #

Works on HP-UX, too. (Without the quotation
marks, the spacing would be different, so
they're clearly getting used.)

> Perhaps an actual failing test program would
> be easier to diagnose than waving hands.

Still true. Working programs do seem to
work, however.