首页 > 解决方案 > 关于 C 中使用 minGW-W64 的 stat 函数的新手问题

问题描述

这是一个非常新手的问题。此代码成功打印文件夹中第一个文件的文件名(包括扩展名),然后暂停,直到 Windows 操作系统说它停止工作。它不打印statinrc也不中断的返回值。它只是停止工作。

我正在使用 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;
}

标签: c

解决方案


推荐阅读