LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-16-2006, 08:17 AM   #16
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30

hello,
i took a look at opendir/readdir/closedir function...
question is how to parse the stream, so that every dir/file becomes an array member.

Last edited by ygloo; 11-16-2006 at 08:21 AM.
 
Old 11-16-2006, 04:50 PM   #17
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Ygloo,

The popen command works just like the fopen. It just opens a pipe and you have to treat it as a text file; that means, you have to "fgets" the contents of the stream into your program and treat each record as a line in your program. The strtok must be used for each field after read the record.
Another thing to note is you are thinking of the tabulated ls, with various files per line. When you execute ls for other output than the console, you'll get the option "-1", which means one file per line.

Last but not least, use the strtok very carefully as it is the once function I know which destroys the source variable because it replaces the delimiter with a null character; also it has an static pointer which stores the position of the next operation. This means you cannot use 2 strtoks at the same time in your program.
 
Old 11-16-2006, 06:39 PM   #18
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
Question

read in a c_tutorial that "fgets" is obsolete... (suggested is "getline")
i made it to the point where script reads from pipe and "strtok" parses the output with delimiter "\n" (newline), so i get directory list...

howto load this data into an array??
i wonder what to do with array size ( every dir has different number of files in it)
one idea is to count how many tokens "strtok" is producing and then to initialise the array...

tux has to think... ... ...

Last edited by ygloo; 11-16-2006 at 06:41 PM.
 
Old 11-16-2006, 06:44 PM   #19
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char delims[] = "\n";
  char *result = NULL;
  FILE *ls_pipe;
  int bytes_read;
  int nbytes = 100;
  char *my_string;


  /* Open pipe */
  ls_pipe = popen ("ls", "r");
  
  /* Check that pipe is non-null, therefore open */
  if (!ls_pipe)
    {
      fprintf (stderr,
               "pipe failed.\n");
      return EXIT_FAILURE;
    }

  /* These 2 lines are the heart of the program. */
  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getdelim (&my_string, &nbytes, "\n", ls_pipe);

  if (bytes_read == -1)
    {
      puts ("ERROR!");
    }
  else
    {
      result = strtok( my_string, delims );
         while( result != NULL )
	    {
               printf( "%s\n", result );
               result = strtok( NULL, delims );
            } 
	          
    }
   
  return 0;
}
 
Old 11-16-2006, 07:02 PM   #20
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Ygloo,

I don't want to flame about fgets and getline but getline is not an ANSI C standard. And both functions serve the same purpose.

Your problem must be resolved with dynamic memory allocation; you need to control the memory available and check if there is enough. Here you should look for "linked lists". The functions you'll need are malloc and free.

The beauty of this language is that there are zillions of ways to obtain the job done.

That is the beginning of the memory leaks in your life.
 
  


Reply

Tags
array, pipe


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 01:06 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration