C++ exec* print to file

Discussion in 'Mac Programming' started by Kirmie, Sep 27, 2005.

  1. Kirmie macrumors newbie

    Joined:
    Sep 27, 2005
    #1
    I've been trying to figure out how to use exec to print a string to a file. Trying tee but I don't want the user to enter it directly, I want to use a variable to input the string. Any help would be greatly appreciated.
     
  2. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    I'm really unclear on what u'r trying to do, but u should be able to use sprintf to format the variable and fprintf to write it to a file from wherever. It's not very C++ but it works.
     
  3. Kirmie thread starter macrumors newbie

    Joined:
    Sep 27, 2005
    #3
    I have to use execlp("filename","name", "argument"...) system call (or any other exec system call) to write to a file. Think sed might do it but can't find enough information on it.
     
  4. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #4
    There is an important saying: the right tools for the right job. Why do you need to use exec to write the file?
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    It seems like a timeless, useless homework assignment because the professor couldn't think of anything that made real world sense.
     
  6. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #6
    Just use /bin/echo and be done with it.

    I'm assuming that the real point of the assignment is to make sure you understand how the exec family works, and the printing requirement is simply to get feedback that you passed the arguments correctly.
     
  7. Kirmie thread starter macrumors newbie

    Joined:
    Sep 27, 2005
    #7
    I'm stil getting confused so let me retry this. I have a string in char buffer[100] and I need to use a execlp call to put buffer on a line in phone.txt. I have to use the variable because of user input and I don't know how to get echo to send the output into phone.txt using execlp.
     
  8. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #8
    One way you can deal with that is to open phone.txt in your main program, using freopen() to redirect standard output there. Then you can do your exec.

    Another way would be to have your exec run /bin/sh instead of the target program directly, and then you can use the normal shell redirection stuff.

    The freopen() approach may go down better with your instructor, because using the shell here introduces an unnecessary process and loses you an opportunity to check for some errors (does the program you're trying to run exist and is it executable?).
     
  9. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #9
    It's simple. You pass your buffer as an argument to /bin/echo command and redirect echo's output to target file. Invoke exec* function and you done. Following is an example:

    // gcc exec.c -o xexec && xexec && cat phone.txt
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>

    int main() {
    char message[] = "301 345-5681\n408 789-2314";
    char* argv[] = {"/bin/sh", "-c", NULL, NULL};
    char* envp[] = {NULL};
    char cmd[1024]; // make it large enough for message
    snprintf(cmd, sizeof(cmd), "echo '%s' >| phone.txt", message);
    argv[2] = cmd;

    printf("ERR: exec returned %d and errno %d\n", execve("/bin/sh", argv, envp), errno);
    return 0;
    }
     
  10. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #10
    Oh man...just looking at C++ code makes my head hurt. I've been spoiled by Obj-C. :rolleyes:
     
  11. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #11
    My code snippet is plain C. How do you rewrite it in Objective-C?
     
  12. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #12
    It already is ObjC.
     
  13. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #13
    Technically any C code is an ObjC. But one can provide the same functionality using Cocoa API. Like in following snippet:

    #import <Foundation/Foundation.h>

    int main(int argc, char** argv)
    {
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];

    NSString* message = @"301 345-5681\n408 789-2314";
    NSString* cmd = [ NSString localizedStringWithFormat:mad:"echo '%@' >| phone2.txt", message ];
    NSArray* args = [ NSArray arrayWithObjects: @"-c", cmd, nil ];

    NSTask* theTask = [ [ NSTask alloc] init ];
    [ theTask setLaunchPath:mad:"/bin/sh" ];
    [ theTask setArguments: args ];
    [ theTask launch ];

    [ pool release ];
    return 0;
    }

    But in this case I do not see a substantial advantage of using Cocoa API.
     
  14. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #14
    Oh, not your code above. Just in general. I think I was just in a bad mood.
     

Share This Page