LinuxQuestions.org
Register a domain and help support LQ
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 01-17-2007, 02:52 PM   #1
rs_vijay
Member
 
Registered: Aug 2006
Location: Allahabad,India
Distribution: Fedora Core 5
Posts: 41

Rep: Reputation: 15
Tried but in vain to list contents of a directory


Hi all,

Having learnt directory too is a interpreted as a file , I tried opening it in vi editor and i found its contents(i mean files n sub-dirs) there , maybe , i thought these strings are symbolic links to the actual contents in memory..

Later, when I tried to print same contents using "cat" it gave me an error that the argument is a directory and hence I wrote a simple C code to open the directory argument as a file (it is ofcourse) n list its contents parsing the file character by char. When i compile n run it with some sub-directory as an argument , unfortunately it didn't print anything.

So this time i edited my code such that it prints the no of bytes too in this (directory) file (just seeking the file pointer to the end n printing its offset using ftell()) n when i run it it gives me the fixed integer "4096" for many directories which i have tried (including the / directory), but still no contents(i.e no bytes) being displayed ..

I felt it crazy n something is there in these directory files which i yet donno ,

as the code appears elementary i don't think its necessary posting it here...

just learnt C in UNIX ( all those editing n using cc,gdb,linking libraries , running executables..) , i am a sort of beginner in UNIX ...
hope i am pretty clear with my doubt.. n any solution is greatly appreciated...

- Vijay

Last edited by rs_vijay; 01-17-2007 at 02:54 PM.
 
Old 01-17-2007, 03:22 PM   #2
mackdav
Member
 
Registered: Feb 2005
Distribution: RHEL, CentOS
Posts: 113

Rep: Reputation: 16
vim interprets directories as files for your convenience to assist you in getting to the file that you actually want. They are not "files". There should be "readdir" functions you can use to read directories.
 
Old 01-17-2007, 03:36 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Quote:
Originally Posted by mackdav
vim interprets directories as files for your convenience to assist you in getting to the file that you actually want. They are not "files". There should be "readdir" functions you can use to read directories.
And, of course, you can get the source for the "readdir" function -- or, perhaps, the "ls" command -- and see how the directory is read.

Note that a directory, like almost everyting in *nix systems, is a file, but some files require "special" handeling. The information need to do so is (I believe) to be found in the POSIX standards for UNIX systems. (That's a guess, since I've never actually looked at the standards.)
 
Old 01-17-2007, 03:59 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Getting the raw bytes of a directory entry wouldn't be so useful, since the contents (and hence what to do with them to achieve some goal) will depend on the underlying filesystem you are using.
 
  


Reply


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
How to list contents of a .tar.gz without unzip? leontini Linux - General 14 04-21-2010 02:07 AM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM
cannot list directory contents kpachopoulos Linux - General 2 11-29-2005 05:24 PM
won't list contents of cdrom gillmb Linux - Newbie 2 10-19-2004 02:09 PM
List the contents of a directory to a file MasterC Linux - General 19 11-26-2002 10:09 AM


All times are GMT -5. The time now is 03:36 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