笔记1-1: 实现一个简单的ls列出目录中的所有项目的程序

本文介绍了一个简单的使用C语言实现的ls程序,该程序通过opendir、readdir和closedir函数来列出指定目录中的所有文件及子目录。适用于学习文件操作的基础示例。

实现一个简单的ls列出目录中的所有项目的程序:

 

#include <apue.h>

#include <dirent.h>

 

int main(int argc, char *argv[])

{

   DIR    * dp;

   struct dirent * dirp;

 

   if (argc != 2)

      err_quit("usage : ls directory_name\n");

   

   if ((dp = opendir(argv[1])) == NULL)

      err_sys("cannot open directory %s\n", argv[1]);

 

   while ((dirp = readdir(dp)) != NULL)

      printf("%s\n", dirp->d_name);

 

   closedir(dp);

   return 0;

}

 

opendir()函数打开一个目录,返回一个指向DIR结构的指针,如果打开失败则返回NULL

函数原型:

DIR*opendir(const char *path);

 

循环调用readdir()函数可以读出DIR结构指向的目录的每个目录项。readdir()成功的话,返回一个目录项结构struct dirent的指针,失败则返回NULL

函数原型:

struct dirent * readdir(DIR * dir);

 

closedir()函数用于关闭参数dir所指的目录流。关闭成功则返回0,失败返回-1

函数原型:

int closedir(DIR *dir);

 

注意:目录文件作为一种文件,在打开必须关闭,否则会由于文件的进程打开文件过多而不能打开新的文件。因此opendir函数和closedir函数同样是配对出现的。

 

注:上面函数功能,在Windows平台下可使用_findfirst(), _findnext(), _findclose() 函数代替。

 

 

 

 

 

 

转载于:https://www.cnblogs.com/gradliang/archive/2013/05/21/3788974.html

[I 2025-06-09 17:38:07.997 ServerApp] Extension package panel.io.jupyter_server_extension took 0.7374s to import [I 2025-06-09 17:38:07.997 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-06-09 17:38:08.004 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-06-09 17:38:08.008 ServerApp] jupyterlab | extension was successfully linked. C:\Users\Lcy\.jupyter\jupyter_notebook_config.py:707: SyntaxWarning: invalid escape sequence '\P' webbrowser.register('chrome',None,webbrowser.GenericBrowser(u'C:\Program Files\Google\Chrome\Application\chrome.exe')) [W 2025-06-09 17:38:08.097 JupyterNotebookApp] 'browser' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release. [I 2025-06-09 17:38:08.097 ServerApp] notebook | extension was successfully linked. [I 2025-06-09 17:38:08.603 ServerApp] notebook_shim | extension was successfully linked. [I 2025-06-09 17:38:08.603 ServerApp] panel.io.jupyter_server_extension | extension was successfully linked. [I 2025-06-09 17:38:08.646 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-06-09 17:38:08.654 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-06-09 17:38:08.654 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-06-09 17:38:08.659 LabApp] JupyterLab extension loaded from D:\anaconda\Lib\site-packages\jupyterlab [I 2025-06-09 17:38:08.659 LabApp] JupyterLab application directory is D:\anaconda\share\jupyter\lab [I 2025-06-09 17:38:08.659 LabApp] Extension Manager is 'pypi'. [I 2025-06-09 17:38:08.854 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-06-09 17:38:08.860 ServerApp] notebook | extension was successfully loaded. [I 2025-06-09 17:38:08.860 ServerApp] panel.io.jupyter_server_extension | extension was successfully loaded. [I 2025-06-09 17:38:08.860 ServerApp] Serving notebooks from local directory: C:\Users\Lcy [I 2025-06-09 17:38:08.860 ServerApp] Jupyter Server 2.16.0 is running at: [I 2025-06-09 17:38:08.860 ServerApp] http://localhost:8888/tree?token=85e2546a71944a477d192d8924fed393c16de926d43341ea [I 2025-06-09 17:38:08.860 ServerApp] http://127.0.0.1:8888/tree?token=85e2546a71944a477d192d8924fed393c16de926d43341ea [I 2025-06-09 17:38:08.860 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [E 2025-06-09 17:38:08.864 ServerApp] Failed to write server-info to C:\Users\Lcy\AppData\Roaming\jupyter\runtime\jpserver-20092.json: PermissionError(13, 'Permission denied') Traceback (most recent call last): File "D:\anaconda\Scripts\jupyter-notebook-script.py", line 10, in sys.exit(main()) ^^^^^^ File "D:\anaconda\Lib\site-packages\jupyter_server\extension\application.py", line 635, in launch_instance serverapp.start() File "D:\anaconda\Lib\site-packages\jupyter_server\serverapp.py", line 3196, in start self.start_app() File "D:\anaconda\Lib\site-packages\jupyter_server\serverapp.py", line 3088, in start_app self.write_browser_open_files() File "D:\anaconda\Lib\site-packages\jupyter_server\serverapp.py", line 2955, in write_browser_open_files self.write_browser_open_file() File "D:\anaconda\Lib\site-packages\jupyter_server\serverapp.py", line 2978, in write_browser_open_file with open(self.browser_open_file, "w", encoding="utf-8") as f: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Lcy\\AppData\\Roaming\\jupyter\\runtime\\jpserver-20092-open.html'尝试了之前的命令还是不能安装
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值