c - 关于 C 中使用 minGW-W64 的 stat 函数的新手问题
问题描述
这是一个非常新手的问题。此代码成功打印文件夹中第一个文件的文件名(包括扩展名),然后暂停,直到 Windows 操作系统说它停止工作。它不打印stat
inrc
也不中断的返回值。它只是停止工作。
我正在使用 minGW-W64。
你能告诉我我做错了什么吗?
谢谢你。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
struct stat *info;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
while ( ep = readdir( dp ) )
{
if ( *(ep->d_name) == '.' ) continue;
printf( "Name : %s\n", ep->d_name );
if ( ( rc = stat( ep->d_name, info ) ) != 0 )
{
printf( "rc : %d\n", rc );
break;
}
printf( "size : %d", info->st_mode );
}
printf( "Broke" );
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
这是根据以下评论修复后可以使用的版本。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
struct stat info;
int rc;
dp = opendir ("./SQLite3");
if (dp != NULL)
{
while ( ep = readdir( dp ) )
{
printf( "Name : %s, ", ep->d_name );
if ( *(ep->d_name) == '.' )
{
printf(" non-useful file\n");
continue;
}
if ( ( rc = stat( ep->d_name, &info ) ) != 0 )
{
printf( "rc : %d\n", rc );
printf( "errno : %d, strerror : %s\n", errno, strerror( errno ) );
break;
}
printf( "mode : %d, size : %d\n", info.st_mode, info.st_size );
}
printf( "Broke" );
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
解决方案
推荐阅读
- if-statement - 在 Jekyll 中有条件地添加数据属性
- python - 如何为 Windows 10 的 Python 3.6.5 安装 Python 包
- c - 是否可以将这种无锁 32 位哈希表算法用于 64 位密钥?
- java - 将视频从 android 设备分享到 Whatsapp
- c# - 无法从自定义类打印 foreach 值
- angular - Chart.js 注释工作但在 Angular 5 中导致控制台错误
- json - VBA 授权 oauth2 API
- python - 在 Python 中解析子标签 XML
- c# - 如何在使用 iTextsharp 签署 pdf 时优化时间
- windows - Packer 错误消息:找不到名为 'packer-virtualbox-iso-# 的注册机器