Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to traverse the files in the directory in C++

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to traverse the files under the directory in C++. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Code first:

# include

# include

Void print_dir (DIR * dir) {

Struct dirent * file

/ / iterate through the contents of the folder

While ((file = readdir (dir))! = NULL) {

Printf ("File location =% ld", telldir (dir))

Printf ("child files:% 20s", file- > d_name)

Printf ("\ n")

}

}

Int main (int argc, const char * argv []) {

Const char * dirPATH = "/ Users/ckend/Documents"

DIR * dir = opendir (dirPATH)

Print_dir (dir)

Return 0

}

The header file you need to use: "dirent.h"

Structure and function to be used: "struct dirent"

"readdir ()"

"opendir ()"

"telldir ()"

Explain it one by one:

Dirent.h, which contains many header files about the functions or structures of directory operations.

Readdir (), from dirent.h, the statement in the program:

While ((file = readdir (dir))! = NULL) {}

Its function is to return a dirent structure pointer to the files in the folder, which checks one by one until there are no more files and NULL is returned.

Opendir (), also from dirent.h, is used to open a folder path and return the DIR* pointer to that folder.

Telldir (), also from dirent.h, gets the current pointer position and returns it.

Struct dirent, this structure also comes from dirent. Its contents are as follows (excerpted from references):

Struct dirent {ino_t diterino; / * inode number * / off_t dailseekoff in the file system; / * related to the location pointer operation of the folder stream * / unsigned short dumbrecline; / * the data length of this record * / unsigned char d_type / * the file types of the current traversal subitems: files, folders, link, socket, etc. * / char d_name [256]; / * the file names of the current traversal subitems * /}. This is the end of how to traverse the files in the directory in C++. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report