簡體   English   中英

打印當前目錄中所有文件的名稱 - c

[英]Print the names of all files in the current directory - c

我需要打印當前目錄中所有文件的名稱。 但我必須只使用系統調用,所以我有SYS_OPEN, SYS_GETDENTS, SYS_READ等。我正在研究Ubuntu。 我該怎么做? 我試着用這個:

system_call(SYS_OPEN, ".", 0, 0777);

然后從STDOUT READ並寫入...但它不起作用。

**我不能使用標准庫。 謝謝!

編輯:示例代碼:**程序集中有一個文件,具有執行調用的函數“系統調用”。 #include“util.h”

#define SYS_WRITE 4

#define SYS_OPEN 5

#define SYS_CLOSE 6

#define SYS_READ 3

#define SYS_LSEEK 19

#define SYS_GETDENTS 141

#define BUF_SIZE 1024

#define STDOUT 1

 struct linux_dirent {
           long           d_ino;
           int         d_off;

           unsigned short d_reclen;

           char           d_name[];

       };         

int main (int argc , char* argv[], char* envp[])

{

   int fd=0;

   int nread;

   char * nread_str;

   char buf[BUF_SIZE];

   struct linux_dirent *d;

           int bpos;

           char d_type;

  fd=system_call(SYS_OPEN,".", 0 , 0);
          for ( ; ; ) {

               nread = system_call(SYS_GETDENTS, fd, buf, BUF_SIZE);

               if (nread == -1)              

        system_call(SYS_WRITE,STDOUT, "error-getdents",BUF_SIZE);

               if (nread == 0)

                   break;

          system_call(SYS_WRITE,STDOUT, "--------------- nread=%d ---------------\n",100);

      nread_str=itoa(nread);      

          system_call(SYS_WRITE,STDOUT, nread_str,100);                      

          system_call(SYS_WRITE,STDOUT, "i-node#  file type  d_reclen  d_off   d_name\n",100);

               for (bpos = 0; bpos < nread;) {

                   d = (struct linux_dirent *) (buf + bpos);

                  /* printf("%8ld  ", d->d_ino);**/

                   d_type = *(buf + bpos + d->d_reclen - 1);


                  /* printf("%4d %10lld  %s\n", d->d_reclen,
                           (long long) d->d_off, (char *) d->d_name);**/
                   bpos += d->d_reclen;
               }
           }

}
  return 0;
}
#include <fstream>
#include <iostream>
#include <string>

#include <dirent.h>

using namespace std;

int main()
  {
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\")) != NULL) {
  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    printf ("%s\n", ent->d_name);
  }
  closedir (dir);
} else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;}
}

閱讀opendir等的手冊頁。

http://linux.die.net/man/3/opendir

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM