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.
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.