Linux/unix reads all the file names under the specified directory __linux

Source: Internet
Author: User
Linux/unix reads all file names under the specified directory call the system function Opendir () and readdir to traverse all the files under a specified directory under Linux/unix and output the file name. The implementation code is as follows:
*
	author:acb0y
	FileName:main.cpp
	Create time:2011 August 1 0:41:18
	version:v1.0
 * * *
#include <iostream>
#include "apue.h"
#include <dirent.h>
using namespace std;

int main (int argc, char * argv[])
{
	DIR *dp;
	struct dirent *dirp;
	if (argc!= 2) 
	{
		err_quit ("Usage:ls directory_name");
	
	Opens the specified directory
	if ((DP = Opendir (argv[1])) = = NULL)
	{
		Err_sys ("can ' t open%s", argv[1]);
	}
	
	Traverse Directory
	while ((DIRP = Readdir (DP))!= NULL)
	{
		printf ("%s\n", dirp->d_name);
	}
	
	Close Directory
	Closedir (DP);
	return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.