- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sprintf does not preserve quotes in c program
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 07:08 AM
тАО10-22-2008 07:08 AM
sprintf does not preserve quotes in c program
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 07:35 AM
тАО10-22-2008 07:35 AM
Re: sprintf does not preserve quotes in c program
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 08:15 AM
тАО10-22-2008 08:15 AM
Re: sprintf does not preserve quotes in c program
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)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 08:48 AM
тАО10-22-2008 08:48 AM
Re: sprintf does not preserve quotes in c program
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 09:00 AM
тАО10-22-2008 09:00 AM
Re: sprintf does not preserve quotes in c program
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 09:21 AM
тАО10-22-2008 09:21 AM
Re: sprintf does not preserve quotes in c program
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 09:49 AM
тАО10-22-2008 09:49 AM
Re: sprintf does not preserve quotes in c program
#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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2008 01:10 PM
тАО10-22-2008 01:10 PM
Re: sprintf does not preserve quotes in c program
#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.