LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-11-2005, 06:03 PM   #1
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Rep: Reputation: 33
How can you get the conten of a folder with C++


Hello everyone.

My question is simple.
If your program is passed a directoy address, how can you make sure it is not invalid.
If it is valid, how can you get the name of all file and subdirectory in this folder.

Thanks for your attention
 
Old 08-11-2005, 06:56 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Look into opendir(), closedir(), readdir(), etc.
 
Old 08-13-2005, 02:58 AM   #3
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Original Poster
Rep: Reputation: 33
Quote:
Originally posted by Matir
Look into opendir(), closedir(), readdir(), etc.
What file do I need inculde in order to use these function.
Please note that I am new to Linux programing, I have no document
about how to use these function.
 
Old 08-13-2005, 09:47 AM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
This should get you started...

Code:
#include <stdio.h>
#include <dirent.h>

main()
{
	DIR* d_str;
	struct dirent* dir;
	
	if((d_str = opendir("/etc")) == NULL)
	{
		printf("Could not open directory");
		exit(1);
	}

	while((dir = readdir(d_str)) != NULL)
	{
		printf("%-32s %d\n", dir->d_name, dir->d_ino);
	}
}
Basically you #include <dirent.h> (look for it on your system... slocate dirent.h). Calling opendir() returns a pointer to a DIR stream, and readdir() returns a pointer to a struct dirent. You call opendir once, and it returns NULL if it can't satisfy your request. You can call readdir repeatedly in a loop to get the entire contents of a directory. The first two entries will be "." and ".." respectively. I just googled for <dirent.h> and found a page by the Open Group with info on the header.


edit: As Matir showed, you have your man pages available which I always forget about too

Code:
man opendir
man closedir
man readdir
man rewinddir
man scandir
etc... As you need to know more you can just follow the trail of man pages.

Last edited by lowpro2k3; 08-13-2005 at 09:57 AM.
 
  


Reply



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
Folder Max Size and Limiting SSH access to home folder. Mefistofeles Linux - General 4 11-26-2005 02:09 PM
permissions difference between /home/..../folder and /root/folder darkleaf Linux - General 3 07-21-2005 05:23 PM
Cant add folder into my shared folder? amer_58 Linux - Newbie 2 04-03-2005 11:35 AM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM
gnome home folder and trash folder ambelos Linux - Software 2 02-24-2004 06:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration