LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2006, 07:08 PM   #1
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
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

Last edited by ygloo; 11-17-2006 at 06:30 AM.
 
Old 11-13-2006, 08:10 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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?
 
Old 11-14-2006, 10:38 AM   #3
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
this is all the code...
i saw how to use strok here:
http://www.cppreference.com/stdstring/strtok.html
 
Old 11-14-2006, 11:00 AM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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)?
 
Old 11-14-2006, 11:15 AM   #5
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
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
 
Old 11-14-2006, 11:17 AM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
its referring to what i said in my previous post, you cant pass ls_pipe/FILE*. they have to be char*'s
 
Old 11-14-2006, 03:43 PM   #7
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
changed "FILE *ls_pipe;"
to "char *ls_pipe;"..
not working...
 
Old 11-14-2006, 05:10 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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
 
Old 11-15-2006, 10:59 AM   #9
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
compile message is:
color.c: In function `main':
color.c:11: warning: passing arg 1 of `strtok' from incompatible pointer type
 
Old 11-15-2006, 11:10 AM   #10
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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?
 
Old 11-15-2006, 11:44 AM   #11
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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.
 
Old 11-15-2006, 12:05 PM   #12
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
i try to pass stream from "popen" directly to "strtok"
 
Old 11-15-2006, 12:30 PM   #13
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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
 
Old 11-15-2006, 12:32 PM   #14
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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:
 
Old 11-15-2006, 10:04 PM   #15
bruce_goose
LQ Newbie
 
Registered: Nov 2006
Posts: 4

Rep: Reputation: 0
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.
 
  


Reply

Tags
array, pipe



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe output of command to php !!! ALInux Programming 7 12-30-2006 12:36 PM
Redirecting sh output to a pipe elmafiacs Programming 0 05-10-2006 01:55 PM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
how to pipe output from make BBB Programming 2 05-20-2005 06:14 AM
Output truncated when using pipe tells Linux - General 1 12-16-2003 08:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration