LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help writing the ls command in C (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-writing-the-ls-command-in-c-4175426526/)

laffytaffykidd 09-10-2012 09:46 AM

Need help writing the ls command in C
 
Hey everybody this is my first post here, and I know that it would be beneficial if I also post what I have tried, but the problem is, I'm not sure where to start.

I basically have to write a program in C based on the ls command (showing the current working directory).

So far, I have only looked up man pages for opendir(), readdir(), stat()...
From there, I am still unsure about how I would write the ls command.
I'm a little new to the C language, but I am a little fluent in C++.

Could someone point me in the right direction?

johnsfine 09-10-2012 09:56 AM

Quote:

Originally Posted by laffytaffykidd (Post 4776909)
I basically have to write a program

That phrase implies a homework assignment.

You need to show a little more effort and/or ask more specific questions to expect homework assistance here.

Quote:

So far, I have only looked up man pages for opendir(), readdir(), stat()...
Are there specific details of those functions that you didn't understand from the man pages? That kind of question is OK to ask, even for homework.

Quote:

From there, I am still unsure about how I would write the ls command.
I'm a little new to the C language, but I am a little fluent in C++.
Does that mean you would know how to do it in C++ ?

You could start with a C++ program and then change each part of the program that does something not available in C to do the equivalent operation in a method supported by C. At each such step, you would still have a testable C++ program. At the end of all such steps you could have a program that works equally well in C++ or C.

Do you know how to use printf in C? printf is the same in C++, so you might even know how to use it there, but in C++ there are usually better alternatives to printf, so you might not.

Quote:

Originally Posted by laffytaffykidd (Post 4776909)
I'm not sure where to start.

If you know any programming at all then how is it possible you don't know where to start? Your program needs to get information and display information. You can't have needed me to tell you to start by dividing your effort into those two areas. You already took the biggest step (or maybe your instructor gave it to you) of selecting functions that are useful for the "get information" part of the task. And you already started by reading the man pages of those functions.


All times are GMT -5. The time now is 09:56 AM.