Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kirmie

macrumors newbie
Original poster
Sep 27, 2005
3
0
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,717
1,891
Lard
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.
 

Kirmie

macrumors newbie
Original poster
Sep 27, 2005
3
0
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,717
1,891
Lard
It seems like a timeless, useless homework assignment because the professor couldn't think of anything that made real world sense.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Kirmie said:
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.
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.
 

Kirmie

macrumors newbie
Original poster
Sep 27, 2005
3
0
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.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Kirmie said:
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.

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?).
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
Kirmie said:
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.
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;
}
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
iMeowbot said:
It already is ObjC.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.