Process Analysis of reading directory file information in Linux operating system

Source: Internet
Author: User
Tags command line

General Process:

1. Add header file Dirent.h

Declares the structure body pointer struct dirent * PTR;

Declares a dir pointer dir * FD;

  2. Analysis command line

3. Open catalog File

Using the Opendir () parameter to parse the second parameter of the command line Argv[1], return the directory file pointer value to the FD

 4. Output redirection

Use the Freopen () parameter to parse the third parameter of the command line argv[2], permission "w+", File descriptor Stdtout

5. Start reading catalog file contents

Using the Loop while (Ptr=readdir (FD)!=null), Readdir () extracts a file item from the catalog file each time, and the pointer moves forward. Until the end of the file returns a null value. The PTR pointer points to the DIRENT structure returned by Readdir (), each time the function call returns a different dirent of the member contents, where the value of the D_name member is the name of the file in the directory that is read each time. Using the original mstat.c in the JoJo to obtain the file information code will ptr->d_name to do fopen (), Fstat () parameters, you can print out the directory of all the files in the information and into the output file.

  6. Close all file and directory file pointers .

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.