LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   store output of cmd in array? (https://www.linuxquestions.org/questions/programming-9/store-output-of-cmd-in-array-101269/)

h/w 10-07-2003 11:44 AM

store output of cmd in array?
 
hello.
im trying to save the output of a command, like "ls" into a char array.
the exec commands return ints, and i dont know how to have the output stored in some array rather than being printed on console.

thanks in advance.

orgcandman 10-07-2003 11:56 AM

if you're trying to execute a separate program and then save that output, exec isn't the command for you unless you fork() first. exec overwrites your program's address space with the new program. you're looking to use something like system("./execute me > foo.txt"); and then read foo.txt for any results.

h/w 10-07-2003 12:28 PM

thank you very much - will do as you suggested. i was trying not to deal with files, and just have the output of the command moved into a variable ... but ill use files for now. thanks again.

Hko 10-07-2003 03:10 PM

By the way, are you talking shell/bash programming here? Or C?

orgcandman 10-07-2003 03:28 PM

heh...I was assuming C.....I get's it's true what they say about assuming....

Aaron

h/w 10-07-2003 03:30 PM

c - u assumed right

jimveta 10-09-2003 08:46 PM

if there are system calls or libc functions that do a subset of some
user command, it may be more convenient ( .. or maybe not ) to just
use those instead of outputting to a text file then parsing it

for example with ls, you can use readdir()


All times are GMT -5. The time now is 01:52 PM.