Display file


 
Thread Tools Search this Thread
Top Forums Programming Display file
# 1  
Old 05-11-2010
Display file

I am making a program in c. I want the progrm to be able to display file names ( like in unix we do long lisiting)

what should i do?

Any help will be appreciated
# 2  
Old 05-11-2010
With the system() command?
The other way is to use functions like opendir, lstat and structs like dirent, stat etc..
Code:
man 2 stat
man dirent

# 3  
Old 05-11-2010
I am going to run this prgm in unix
# 4  
Old 05-11-2010
Use and build from this small code itself ...

Code:
/*
** mydirDisplay.c
*/

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main(int argc, char *argv[])
{
   struct dirent *ent;
   DIR *dir;

   if (argc == 2) {
       if ((dir = opendir(argv[1]))) {
           while((ent = readdir(dir)) != NULL)
               fprintf(stdout, "%s\n", ent->d_name);
       } else
           fprintf(stderr, "Error opening directory %s\n", argv[1]);
   }

   return 0;
}


What this code does it that it takes the command line argument as the input directory and opens the same using opendir() system call; on sucess of this sys call it reads the directory entaties using readdir() syscall and prints the same on the stdout untill it encounters NULL.

Now what you need to do is run the following command on your Linux system prompt:

$ gcc mydirDisplay.c -o myls

and
$ ./myls <some directory Path>

You may also put it at either /bin directory, if you have root access, or in any directory but add the path to your environmental PATH variable; and this becomes your command.

Do add features like other swtiches of the ls command (e.g. -l, -a etc...) and instead of depending on the argc, argv only, try using getopt() ... look at the man pages of getopt() and also improve the display fprintf(stdout, ...) to look like what 'ls' prints on the stdout as a task for you. Let me know if you have any other queries.

Last edited by Praveen_218; 05-11-2010 at 12:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

2. Shell Programming and Scripting

File Name needs to display

I am new to shell scripting plz help me to get solution for below requirement - If name.sh file is present, message will be displayed as “name.sh is present” else "name.sh is not present" I have tested below script but getting error. #!/bin/ksh file_found=`ls name.sh` found=`echo $?` if... (6 Replies)
Discussion started by: rahulbahulekar
6 Replies

3. Homework & Coursework Questions

adding rows of a file and display in another file

How to add these all ( 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25) in the script and How to calculate the GPA?? :wall: For example a course record file can be like this: COURSE NAME: Operating Systems CREDITS: 4 123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25 243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15... (1 Reply)
Discussion started by: poonam29
1 Replies

4. Shell Programming and Scripting

adding rows of a file and display in another file

How to add these all ( 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25) in the script and How to calculate the GPA?? :wall: For example a course record file can be like this: COURSE NAME: Operating Systems CREDITS: 4 123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25 243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15... (1 Reply)
Discussion started by: poonam29
1 Replies

5. Homework & Coursework Questions

Display file name

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have a directory called "Month" which has 12 files named on months. /home/months> touch Jan Feb Mar Apr May... (6 Replies)
Discussion started by: manishdivs
6 Replies

6. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

7. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

8. Shell Programming and Scripting

Display File

I have a file xx like this abc abcd abc abcd abcd efgh ijkl mnop qrst uv wxyz Now I want to search for "abcd". After the last found, it will display the remaining of the file. So here I am expecting something like abcd efgh ijkl mnop qrst uv wxyz (5 Replies)
Discussion started by: joy_deep
5 Replies

9. UNIX for Dummies Questions & Answers

Display File

I have a file xx like this abc abcd abc abcd abcd efgh ijkl mnop qrst uv wxyz Now I want to search for "abcd". After the last found, it will display the remaining of the file. So here I am expecting something like abcd efgh ijkl mnop qrst uv wxyz (2 Replies)
Discussion started by: joy_deep
2 Replies
Login or Register to Ask a Question