2014-09-05 73 views
-1

首先,对于我命名的错误标题抱歉。C++显示目录内容问题

这是我刚刚问的问题:

Display files contain inside a particular directory by using C++ in LINUX

这是我指的是源:

Reading The Contents of Directories

THREAD (C Programming)有像我一样的输出。

文件系统文件夹内容

- test.txt 
- abc.txt 
- item.txt 
- records.txt 

的main.cpp

#include <iostream> 
#include <dirent.h> 
using namespace std; 

int main() 
{ 
    Dir* dir = opendir("/home/user/desktop/TEST/FileSystem"); 
    struct dirent* entry; 

    cout<<"Directory Contents: "<<endl; 
    while((entry = readdir(dir)) != NULL) 
    { 
     cout << "%s " << entry->d_name << endl; 
    }  
} 

输出

Directory Contents: 

%s .. 
%s item.txt 
%s test.txt 
%s records.txt 
%s . 
%s abc.txt 

我的主要问题是,为什么它会显示 “..” 和 “”在OUTPUT上。为什么它会在那里,有什么特别的意义/目的?我如何摆脱这一点,只在文件夹中显示文件ONLY

非常感谢你们回答我的问题。我希望你们不介意我问很多问题。

+2

'.' - 当前目录,'..' - 家长目录 – 2014-09-05 13:31:26

+0

@PiotrS。注意,感谢您的信息:) – J4X 2014-09-05 13:39:20

回答

0

在Unix和Windows中,所有的目录总是包含两个条目"."(目录本身)和".."它是父级(或者它本身,在极少数情况下没有父级)。在Unix下,通常的惯例是名称以'.'开头的目录是“隐藏的”,并且不会显示,但这取决于显示程序;当你阅读一个目录时,你仍然可以看到它们。如果要遵守这个约定,在循环简单if是所有你需要:

dirent* entry = readdir(dir); 
while (entry != nullptr) { 
    if (entry->d_name[0] != '.') { 
     std::cout << entry->d_name << std::endl; 
    } 
    entry = readdir(dir); 
} 
+0

谢谢你的回答! :) @詹姆斯Kanze – J4X 2014-09-06 02:35:21