ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
data coming from a pipe is directory list... ( command "ls" ),
i try to put every filename into an array...
Code:
# include <stdio.h>
int main()
{
FILE *ls_pipe;
char delim[] = " "; // this whitespace??
char *split_input = NULL;
ls_pipe = popen ("ls", "r");
split_input = strtok (ls_pipe, delim);
while (split_input != NULL)
{
printf("split_input is \"%s\"\n", split_input);
split_input = strtok(NULL, delim);
}
pclose (ls_pipe);
return 0;
}
it compiles with this message:color.c:
In function `main':
color.c:10: warning: assignment makes pointer from integer without a cast
color.c:14: warning: assignment makes pointer from integer without a cast
the signature of strtok is: char * strtok ( const char * string, const char * delimiters );
but when you first call it you call it with string (ls_pipe) being a FILE*, is this your problem?
also are you using this without includeing string.h? are you excluding any code in your post?
strtok is in the string.h file, i cant compile this because it gives errors saying it doesnt know what strtok is. i dont see how you dont get these errors?! do #include<string.h> and see if anything changes..
again, for the first time when you use strtok you call it as such:
(FILE*, char*);
but it wants:
(const char* string, const char* delimiters);
can you upload and send a link this file (color.c)?
i posted all file code above
after including "string.h" there is this message:
color.c:11: warning: passing arg 1 of `strtok' from incompatible pointer type
popen gives you a file pointer. But that doesn't contain any actual data that you care about. You have to read from the file, and then pass that data to strtok.
The following example demonstrates the use of popen() and pclose() to execute the command ls * in order to obtain a list of files in the current directory:
you may have your reasons for doing this with the popen command - but have you come across the opendir/readdir/closedir commands ? I feel they give you much more control than token seeking does.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.