stat&S_IFREG in C ++

stat&S_IFREG in C ++,第1张

概述stat&S_IFREG in C ++

voID fun1(char *fl){ //flnamep : stores the path of our directory DIR *dip; struct dirent *dit; dip = opendir(fl); if (dip==NulL) {cerr<<"Errorn";exit(-1);} while ((dit=readdir(dip))) { string trun = (dit->d_name); struct stat buff; stat(dit->d_name,&buff); if (((buff.st_mode & S_IFREG)==S_IFREG)) {cout<<"file"<<endl;} else if (((buff.st_mode & S_IFDIR)==S_IFDIR)) {cout<<"Dir"<<endl;} } closedir(dip); }

代码不区分目录和文件。 我错过了什么? 我不能使用Boost或其他STL。 只有C Posix支持的文件。 需要知道我是错的。

按照答案更新代码

DIR *dip; struct dirent *dit; dip = opendir(flnamep); if (dip==NulL) {cerr<<"Errn";exit(-1);} while ((dit=readdir(dip))) { string trun = (dit->d_name); string fullpath = flnamep; fullpath+='/'; fullpath+=trun; if((trun==".") || (trun=="..")) {cout<<"";} else { struct stat buff; stat(dit->d_name,&buff); if (((buff.st_mode & S_IFDIR)==S_IFDIR)) {cout<<"Dir"<<endl;} else {cout<<"file"<<endl;} }

C ++访问主外部的命令行参数?

如何在linux上正确设置串行通信

为什么linux转储干净MAP_ANONYMOUS内存页面到核心转储?

信号处理器与事件处理程序

奇怪的posix消息队列链接问题 – 有时它不正确链接

Vista中的升级过程不会覆盖文件

特定于Cell linux环境的GDB文档在哪里?

使用共享variablES10 pthreads

在linux下共享内存映射

键盘笔画与windows服务

我怀疑ENOENT (没有这样的文件)实际上失败,所以buff不包含任何有用的东西。

stat(dit->d_name,&buff); /* dirent.d_name is just the name,not the full path */

你可能想连接fl,"/",d_name 。 但首先检查stat返回的stat 。

总结

以上是内存溢出为你收集整理的stat&S_IFREG in C ++全部内容,希望文章能够帮你解决stat&S_IFREG in C ++所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1159665.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-01
下一篇2022-06-01

发表评论

登录后才能评论

评论列表(0条)

    保存