Thread: Escape sequence for &

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Escape sequence for &

    I'm wanting to do the following:

    Code:
    sprintf (cmd,"ls %s > \\tmp\\out.tmp \&", stuff);
    system( cmd );
    However I'm getting this error when compiling

    warning: unknow escape sequence '\&'

    What is the correct escape sequence for adding & using sprintf?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I can't make out what you need "bitwise and" for from your post

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You don't need to escape ampersands.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I didn't know & was an illegal string character and you needed an escape sequence.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Quote Originally Posted by SlyMaelstrom
    I didn't know & was an illegal string character and you needed an escape sequence.
    This code is to run on linux. The '&' is so a new process is created and my program can keep on processing instead of waiting for the other process to return.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You still don't need to escape the ampersand and the path to your output file should be "/tmp/out.tmp" instead of "\\tmp\\out.tmp".
    The ampersand won't help you if you want your program to continue without waiting for the other process to finish. You'll need an asynchronous function for that.

  7. #7
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I think spawn works in linux
    If so, http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Quote Originally Posted by MadCow257
    I think spawn works in linux
    If so, http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Yes I wanted to use the spawnl function...but ubuntu doesn't have the process.h header. I believe this has been a problem on other dist's also. BTW...I'm a very very newbie to programming in general, as you've probably gathered - which is why I'm trying to do things the easy way for now (eg system instead of exec() or spawl())

    Thanks

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > which is why I'm trying to do things the easy way for now
    One of the things of being "easy" is that it has reduced functionality over say execl (there is no spawn in Linux as far as I know). If you want to do something complex, then you have to get to grips with fork()/execl().

    Besides, you got all the slashes wrong.

    sprintf (cmd,"ls %s > /tmp/out.tmp &", stuff);
    ought to work

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Quote Originally Posted by Salem
    > which is why I'm trying to do things the easy way for now
    One of the things of being "easy" is that it has reduced functionality over say execl (there is no spawn in Linux as far as I know). If you want to do something complex, then you have to get to grips with fork()/execl().

    Besides, you got all the slashes wrong.

    sprintf (cmd,"ls %s > /tmp/out.tmp &", stuff);
    ought to work

    Yeah I realised the slashes were wrong after posting...thanks for the response. Once I finish this app I guess I'll try and reimplement some of these things using exec/fork.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Weird problem: error C2017: illegal escape sequence
    By h3ro in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2008, 07:29 PM
  3. Escape sequence of interest?
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 09:28 AM
  4. escape sequence
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 08-01-2002, 08:36 AM
  5. escape sequence "\ooo" and "\xhhh"
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2001, 09:30 PM