如何检查一个文件是否是C ++中的常规文件? [英] How do i check if a file is a regular file in C++?

查看:314
本文介绍了如何检查一个文件是否是C ++中的常规文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个文件是一个普通文件(而不是一个目录,一个管道等),我该如何检查C ++?我需要一个函数isFile()。

  DIR * dp; 
struct dirent * dirp; ((dirp = readdir(dp))!= NULL){
if(isFile(dirp)){
cout< 是一个文件! << ENDL;
i ++;



$ b

我试过比较dirp-> d_type(无符号字符)0x8,但似乎不能通过不同的系统进行移植。

解决方案

您需要在文件上调用stat(2),然后在st_mode上使用S_ISREG宏。


How do i check in C++ if a file is a regular file (and is not a directory, a pipe, etc.)? I need a function isFile().

DIR *dp;
struct dirent *dirp;

while ((dirp = readdir(dp)) != NULL) {
if ( isFile(dirp)) {
     cout << "IS A FILE!" << endl;
i++;
}

I've tried comparing dirp->d_type with (unsigned char)0x8, but it seems not portable through differents systems.

解决方案

You need to call stat(2) on the file, and then use the S_ISREG macro on st_mode.

这篇关于如何检查一个文件是否是C ++中的常规文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆