⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 第4章 数据文件.txt

📁 我自己整理的c语言教程 来自 c语言之家
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include <direct.h>
#include <dos. h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK
void main(void);
void main(void)
{
      FILE_BLOCK f_block;  /* Define the find-t structure variable * /
      int ret-code;        / * Define a variable to store return codes * /
      int hour;            / * We're going to use a 12-hour clockl * /
      char * am_pm;        / * Used to print "am" or "pm" * /

      printf("\nDireetory listing of all files in this directory:\n\n");
      / * Use the ' *. * ' file mask and the 0xFF attribute mask to list
           all files in the directory, including system files, hidden
           files, and subdirectory names.  * /

      ret_code = _dos_findfirst(" *.* ", 0xFF, &f_block);
/* The_dos_findfirst() function returns a 0 when it is successful
  and has found a valid filename in the directory.  * /
    while (ret_code == 0)
    {
         / * Convert from a 24-hour format to a 12-hour format.  * /
         hour = (f_block. wr_time>>11);
         if (hour > 12)
         {
              hour = hour - 12;
              am_pm = "pm";
         }
         else
              am_pm="am";
         / * Print the file's name, date stamp, and time stamp.  * /
         printf("%-12s %2d/%2d/%4d  %2d:%2d:%02d %s\n",
                      f_block.name,                        / * name * /
                      (f-block.wr_date >> 5) & 0x0F,       / * month * /
                      (f_block.wr_date) & 0x1F,            / * day   * /
                      (f_block.wr_date >> 9) + 1980 ,      / * year * /
                      hour,                                / * hour * /
                      (f-block. wr_time >> 5) & 0x3F,      / * minute * /
                      (f_block. wr_time & 0x1F) * 2,       / * seconds * /
                      am_pm);
         /* Use the _ dos_findnext() function to look
              for the next file in the directory.  * /
  
         ret_code = _dos_findnext (&f_block);
    }
    printf("\End of directory listing. \n" );
}

   请注意,为了获得时间变量和日期变量的各个元素,要进行大量移位操作和位处理操作,如果你非常讨厌这些操作,你可以自己定义一个find_t这样的结构,并为C语言定义的find_t结构和你自己定义的结构创建一个共用体(请看下例),从而改进上例中的代码。    

/ * This is the find_t structure as defined by ANSI C.  * /
struct find_t
{
     char reserved[21];
     char attrib;
     unsigned wr_time;
    unsigned wr_date;
     long size;
     char name[13];
/ * This is a custom find_t structure where we
     separate out the bits used for date and time.  * /
struet my_find_t
{
     char reserved[21];
     char attrib;
     unstgned seconds: 5;
     unsigned minutes: 6;
     unsigned hours: 5;
     unsigned day: 5;
     unstgned month: 4;
     unsigned year: 7;
     long size;
     char name[13];
}

/* Now, create a union between these two strucures
   so that we can more easily access the elements of
     wr_date and wr_time.  * /

union file_info
{
      struct find_t ft;
      struct my_find_t mft;
}

    用上例中的自定义结构和共用体,你就可以象下例这样来抽取日期变量和时间变量的各个元素,而不必再进行移位操作和位处理操作了:

...
file_info my_file;
...
printf(" %-12s %2d/%2d/%4d %2d: %2d: %2d %s\n",
             my_file, mfr.name,               / * name       * /
             my-file, mfr.month,              / * moth       * /
             my_file, mfr.day,                / * day        * /
             (my-file. mft.year + 1980),      / * year       * /
             my-file, raft. hours,            / * hour       * /
             my- file. mfr. minutes,          / * minute     * /
             (my_file. mft. seconds * 2),     / * deconds    * /
             am_pm);

    请参见:
    4.8  怎样列出某个目录下的文件?
    4.10 怎样对某个目录下的文件名进行排序?
    4. 11 怎样判断一个文件的属性?

    4.10  怎样对某个目录下的文件名进行排序?
    在4.8的例子中,用_dos_findfirst()和_dos_findnext()函数遍历目录结构,每找到一个文件名,就把它打印在屏幕上,因此,文件名是逐个被找到并列出来的。
    当你对某个目录下的文件名进行排序时,这种逐个处理的方式是行不通的。你必须先将文件名存储起来,当所有的文件名都找到后,再对它们进行排序。为了完成这项任务,你可以建立一个指向find_t结构的指针数组,这样,每找到一个文件名,就可以为相应的find_t结构分配一块内存,将其存储起来。当所有的文件名都找到后,就可以用qsort()函数按文件名对所得到的find_t结构数组进行排序了。
    qsort()函数是一个标准C库函数,它有4个参数:指向待排数组的指针,待排元素的数目,每个元素的大小,指向用来比较待排数组中两个元素的函数的指针。比较函数是你要提供的一个用户自定义函数,根据所比较的第一个元素是大于、小于或等于第二个元素,它将返回一个大于、小于或等于0的值。
    请看下例:
#include <stdio.h>
#include <direct.h>
#include <dos.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK ;
int sort_files(FILE_BLOCK * * , FILE-BLOCK * * );
void main(void);
void main(void)
{
      FILE_BLOCK f_block;        /* Define the find_t structure variable * /
      int ret_code;              /* Define a variable to store the retur
                                                  codes * /
      FILE_BLOCK * * file_block; /* Used to sort the files * /
      int file_count;            / * Used to count the flies * /
      int x;                     / * Counter variable * /
      file_count = -1;
      / * Allocate room to hold up to 512 directory entries.  * /
      file_list = (FILE_BLOCK * * ) malloc(sizeof(FILE_BLOCK * ) * 512);
      printf("\nDirectory listing of all files in this directory ; \n\n");
      / * Use the " *. * " file mask and the 0xFF attribute mask to list
            all files in the directory, including system files, hidden
            files, and subdirectory names.  * /
      ret_code = _dos_findfirst(" *.* ", 0xFF, &f_block);
      / * The _dos_findfirst() function returns a 0 when it is successful
          and has found a valid filename in the directory.  * /
      while (ret_code == 0 && file_count < 512)
      {
           / * Add this filename to the file list * /
           file_list[++ file_count] =
                 (FILE_BLOCK * ) malloc (sizeof(FILE_BLOCK));
            * flile_list[file_count] = f_block;
           /* Use the _dos_findnext() function to look
                for the next file in the directory.  * /
           ret_code = _dos_findnext (&f_block);
      }
      /* Sort the files * /
      qsort(file_list, file_count, sizeof(FILE_BLOCK * ), sort_files);
       / * Now, iterate through the sorted array of filenames and
            print each entry.  * /
      for (x=0; x<file_count; x++)
      {
           printf(" %-12s\n", file_list[x]->name);
      }
      printf("\nEnd of directory listing. \n" );
}
int sort_files(FILE_BLOCK* * a, FILE_BLOCK* * b)
{
      return (strcmp((*a)->name, (*b)->name));
}

    在上例中,由用户自定义的函数sort_files()来比较两个文件名,它的返回值实际就是标准C库函数strcmp()的返回值。只要相应地改变sort_files()函数的操作对象,上例就可按日期、时间或扩展名进行排序。    

    请参见:
    4.8  怎样列出某个目录下的文件?
    4.9  怎样列出一个文件的日期和时间?
    4. 11 怎样判断一个文件的属性?

 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -