LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pipe output loaded into an array (https://www.linuxquestions.org/questions/programming-9/pipe-output-loaded-into-an-array-501299/)

ygloo 11-13-2006 07:08 PM

pipe output loaded into an array (c)
 
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

nadroj 11-13-2006 08:10 PM

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?

ygloo 11-14-2006 10:38 AM

this is all the code...
i saw how to use strok here:
http://www.cppreference.com/stdstring/strtok.html

nadroj 11-14-2006 11:00 AM

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

ygloo 11-14-2006 11:15 AM

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

nadroj 11-14-2006 11:17 AM

its referring to what i said in my previous post, you cant pass ls_pipe/FILE*. they have to be char*'s

ygloo 11-14-2006 03:43 PM

changed "FILE *ls_pipe;"
to "char *ls_pipe;"..
not working...

nadroj 11-14-2006 05:10 PM

whats not working now? same error? different error? if you want help you need to be specific

i dont have access to a linux machine so i cant test this code

ygloo 11-15-2006 10:59 AM

compile message is:
color.c: In function `main':
color.c:11: warning: passing arg 1 of `strtok' from incompatible pointer type

nadroj 11-15-2006 11:10 AM

can you make another char* but make it const and set it to the one your passing? pass this new const char* instead.. i dont see why it wouldnt work.

also if you changed your FILE* to char* are you still doing the popen call? are you not getting errors there?

orgcandman 11-15-2006 11:44 AM

You need to reread your man pages.

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.

ygloo 11-15-2006 12:05 PM

i try to pass stream from "popen" directly to "strtok"

nadroj 11-15-2006 12:30 PM

you cant give strtok a (file) stream. if this isnt what you mean post code to go with it as its usually easier to understand

nadroj 11-15-2006 12:32 PM

check out the example here: http://www.opengroup.org/onlinepubs/...ons/popen.html
Quote:

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:

bruce_goose 11-15-2006 10:04 PM

Hi Ygloo,

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.

Cheers,

Bruce.


All times are GMT -5. The time now is 06:14 PM.