dir功能如何实现?

cuishijie869335 2004-12-16 10:25:37
各位:我在编一个类似cmd的软件。
我想用c++语言实现类似dos命令中的dir(或者是linux下的ls)命令,该如何实现?
...全文
345 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowbirdfly 2005-08-31
  • 打赏
  • 举报
回复
学习~
sky_cool 2005-08-31
  • 打赏
  • 举报
回复
#include<stdlib.h>
int main()
{
system("ls");
}

http://blog.csdn.net/sky_cool/
上有UNIX下C代码自行实现
spacraft 2005-08-31
  • 打赏
  • 举报
回复
具体请see busybox源代码
下载地址:
http://www.busybox.net/downloads/
spacraft 2005-08-31
  • 打赏
  • 举报
回复
/*
display by column original ideas from ls applet,
very optimize by my :)
*/
static void showfiles(char **matches, int nfiles)
{
int ncols, row;
int column_width = 0;
int nrows = nfiles;

/* find the longest file name- use that as the column width */
for (row = 0; row < nrows; row++) {
int l = strlen(matches[row]);

if (column_width < l)
column_width = l;
}
column_width += 2; /* min space for columns */
ncols = cmdedit_termw / column_width;

if (ncols > 1) {
nrows /= ncols;
if(nfiles % ncols)
nrows++; /* round up fractionals */
column_width = -column_width; /* for printf("%-Ns", ...); */
} else {
ncols = 1;
}
for (row = 0; row < nrows; row++) {
int n = row;
int nc;

for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++)
printf("%*s", column_width, matches[n]);
printf("%s\n", matches[n]);
}
}
spacraft 2005-08-31
  • 打赏
  • 举报
回复
extern int ls_main(int argc, char **argv)
{
struct dnode **dnd;
struct dnode **dnf;
struct dnode **dnp;
struct dnode *dn;
struct dnode *cur;
long opt;
int nfiles = 0;
int dnfiles;
int dndirs;
int oi;
int ac;
int i;
char **av;
#ifdef CONFIG_FEATURE_AUTOWIDTH
char *tabstops_str = NULL;
char *terminal_width_str = NULL;
#endif

#ifdef CONFIG_SELINUX
is_flask_enabled_flag = is_flask_enabled();
#endif

all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
| TIME_MOD
#endif
#ifdef CONFIG_FEATURE_LS_SORTFILES
| SORT_NAME | SORT_ORDER_FORWARD
#endif
;

#ifdef CONFIG_FEATURE_AUTOWIDTH
/* Obtain the terminal width. */
get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
/* Go one less... */
terminal_width--;
#endif

#ifdef CONFIG_FEATURE_LS_COLOR
if (isatty(STDOUT_FILENO))
show_color = 1;
#endif

/* process options */
#ifdef CONFIG_FEATURE_AUTOWIDTH
opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str);
if (tabstops_str) {
tabstops = atoi(tabstops_str);
}
if (terminal_width_str) {
terminal_width = atoi(terminal_width_str);
}
#else
opt = bb_getopt_ulflags(argc, argv, ls_options);
#endif
for (i = 0; opt_flags[i] != (1U<<31); i++) {
if (opt & (1 << i)) {
unsigned int flags = opt_flags[i];
if (flags & LIST_MASK_TRIGGER) {
all_fmt &= ~LIST_MASK;
}
if (flags & STYLE_MASK_TRIGGER) {
all_fmt &= ~STYLE_MASK;
}
#ifdef CONFIG_FEATURE_LS_SORTFILES
if (flags & SORT_MASK_TRIGGER) {
all_fmt &= ~SORT_MASK;
}
#endif
if (flags & DISP_MASK_TRIGGER) {
all_fmt &= ~DISP_MASK;
}
#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
if (flags & TIME_MASK_TRIGGER) {
all_fmt &= ~TIME_MASK;
}
#endif
if (flags & LIST_CONTEXT) {
all_fmt |= STYLE_SINGLE;
}
#ifdef CONFIG_FEATURE_HUMAN_READABLE
if (opt == 'l') {
all_fmt &= ~LS_DISP_HR;
}
#endif
all_fmt |= flags;
}
}

/* sort out which command line options take precedence */
#ifdef CONFIG_FEATURE_LS_RECURSIVE
if (all_fmt & DISP_NOLIST)
all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
#endif
#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
if (all_fmt & TIME_CHANGE)
all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
if (all_fmt & TIME_ACCESS)
all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
#endif
if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
#ifdef CONFIG_FEATURE_LS_USERNAME
if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
#endif

/* choose a display format */
if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
#if STYLE_AUTO != 0
all_fmt = (all_fmt & ~STYLE_MASK)
| (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
#else
all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE);
#endif

/*
* when there are no cmd line args we have to supply a default "." arg.
* we will create a second argv array, "av" that will hold either
* our created "." arg, or the real cmd line args. The av array
* just holds the pointers- we don't move the date the pointers
* point to.
*/
ac = argc - optind; /* how many cmd line args are left */
if (ac < 1) {
av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *)));
av[0] = bb_xstrdup(".");
ac = 1;
} else {
av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *)));
for (oi = 0; oi < ac; oi++) {
av[oi] = argv[optind++]; /* copy pointer to real cmd line arg */
}
}

/* now, everything is in the av array */
if (ac > 1)
all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */

/* stuff the command line file names into an dnode array */
dn = NULL;
for (oi = 0; oi < ac; oi++) {
char *fullname = bb_xstrdup(av[oi]);

cur = my_stat(fullname, fullname);
if (!cur)
continue;
cur->next = dn;
dn = cur;
nfiles++;
}

/* now that we know how many files there are
** allocate memory for an array to hold dnode pointers
*/
dnp = dnalloc(nfiles);
for (i = 0, cur = dn; i < nfiles; i++) {
dnp[i] = cur; /* save pointer to node in array */
cur = cur->next;
}

if (all_fmt & DISP_NOLIST) {
#ifdef CONFIG_FEATURE_LS_SORTFILES
shellsort(dnp, nfiles);
#endif
if (nfiles > 0)
showfiles(dnp, nfiles);
} else {
dnd = splitdnarray(dnp, nfiles, SPLIT_DIR);
dnf = splitdnarray(dnp, nfiles, SPLIT_FILE);
dndirs = countdirs(dnp, nfiles);
dnfiles = nfiles - dndirs;
if (dnfiles > 0) {
#ifdef CONFIG_FEATURE_LS_SORTFILES
shellsort(dnf, dnfiles);
#endif
showfiles(dnf, dnfiles);
}
if (dndirs > 0) {
#ifdef CONFIG_FEATURE_LS_SORTFILES
shellsort(dnd, dndirs);
#endif
showdirs(dnd, dndirs, dnfiles == 0);
}
}
return (status);
}
vsdkrepent 2005-08-29
  • 打赏
  • 举报
回复
上楼的...dir.h 头文件那里找回来的???还是自己写的???
anyV 2005-08-28
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <dir.h>

int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
getch();

return 0;
}
dragonzxh 2004-12-16
  • 打赏
  • 举报
回复
bool FindFile(CString strDir, CString strFileNameExt)
{
WIN32_FIND_DATA FileData;
HANDLE hSearch;
BOOL fFinished = FALSE;
CString SearchPath=strDir+"\\"+strFileNameExt;
hSearch = FindFirstFile(SearchPath, &FileData);
if(hSearch==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(L"Cannot Find any mp3 file.");
return false;
}
PlayList.AddTail(strDir+"\\"+FileData.cFileName);
while(!fFinished)
{
if (FindNextFile(hSearch, &FileData))
PlayList.AddTail(strDir+"\\"+FileData.cFileName);
else
fFinished = TRUE;
}
FindClose(hSearch);
return true;
}

PlayList是个CStringList,保存文件名.
dragonzxh 2004-12-16
  • 打赏
  • 举报
回复
WIN32_FIND_DATA
sharkhuang 2004-12-16
  • 打赏
  • 举报
回复
opendir
readdir
stevens2009 2004-12-16
  • 打赏
  • 举报
回复
大概这样吧

main(int argc char *argv[])
{
....
if (argv[1]等于"dir" )
{
获取当前目录;
打印出所有文件名;
}

}
realmz 2004-12-16
  • 打赏
  • 举报
回复
API
HANDLE FindFirstFile(
LPCTSTR lpFileName, // file name
LPWIN32_FIND_DATA lpFindFileData // data buffer
);
prostar 2004-12-16
  • 打赏
  • 举报
回复
看你要在那一层上去实现?

要在最底层实现的话,首先你要有读取硬盘物理扇区的函数,接着读取Sector 0,得到BPB和EBPB。然后里面的参数的值计算Root entry sectors和Data entry sectors以及一些其他的参数。再读取root到内存,接着就象链表一样不停的遍历整个硬盘吧。呵呵。

如果想要高一层上实现,那么系统提供的FindFirst和FindNext就足够了

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧