SEE EDIT
I execute the command "ls" with popen and then I get the output of it with fgets. What I want to do is separate each file/dir name and move it into an array of strings.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(void){
char op[BUFSIZ];
char words[BUFSIZ][BUFSIZ];
int i = 0, j = 0, cnt = 0;
FILE *ls = popen("ls", "r");
// split files and dirs names
fgets(op, strlen(op), ls);
for(i=0;i<=(strlen(op));i++) {
if (op[i] == ' ' || op[i] == '\0') {
words[cnt][j] = '\0';
cnt++;
j = 0;
}
else {
words[cnt][j] = op[i];
j++;
}
}
pclose(ls);
while(words[i]!=NULL){
printf("%s", words[i]);
}
}
EDIT
here's the updated code.
Replaced sizeof with strlen, replaced BUFSIZ, added i++ in the while
loop
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(void){
char op[100];
char words[100][100];
int i=0, j=0, cnt=0;
FILE *ls = popen("ls", "r");
// split files and dirs names
fgets(op, sizeof(op), ls);
printf("%s", op);
for(i=0;i<=(strlen(op));i++) {
if (op[i] == ' ' || op[i] == '\0') {
words[cnt][j] = '\0';
cnt++;
j = 0;
}
else {
words[cnt][j] = op[i];
j++;
}
}
pclose(ls);
while(words[i]!=NULL){
printf("%s", words[i]);
i++;
}
return 0;
}