![]() |
in C, Assigning output of system() to a variable
I'm new to C and Linux and I am confused on how things work. I tried to search but I don't really know if I'm searching for the right thing because lots of other things come up.
Currently, I figured out that I can use shell commands inside C by using the method system(). They display out on the screen fine, but my problem is that I need to assign them to a variable, and not have them display on the screen (yet). I'm planning to put the output of "ls -l" to a variable. I'm thinking of an array of strings (I think there are no strings, but an array char, so I guess this is an array of array of chars?) which will contain every line of the "ls -l" command except for the first line. (I think I need it in an array because I'm supposed to scroll through it in a menu-like style where I'm following example 18 on this link: http://www.tldp.org/HOWTO/NCURSES-Pr...tml#MENUBASICS but I think it's another question for another time, hopefully I can figure it out and not have to though, hahah) What I have is this, but it doesn't work. Code:
#include <stdlib.h>Code:
system.c: In function 'main': |
Two things. If a function returns a pointer-to-char (dynamically allocated or static) and you want to assign the return value to a variable you do:
Code:
char *ptr = some_func_returning_pointer_to_char();Code:
$ man 3 systemThe system() call is part of standard C (and C++ of course) but how it operates and what you can do with the processes it spawns is platform-dependent. |
man popen
I think this does what you are looking for. |
popen example:
Code:
#include <stdio.h> |
Thanks for the answers, I have gotten what I needed. Figured out char arrays (strings) and casting char to int in the process.
This is what I came up with: Code:
#include <ncurses.h> |
| All times are GMT -5. The time now is 02:29 AM. |