首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么输入一个文件夹地址,程序调出地址然后打印到屏幕

2013-01-28 
如何输入一个文件夹地址,程序调出地址然后打印到屏幕?我目前的思路是用system来输入地址,但是我不知道如何

如何输入一个文件夹地址,程序调出地址然后打印到屏幕?
我目前的思路是用system来输入地址,但是我不知道如何通过键盘输入,然后程序调取,再打印出文件夹列表。
请给个列子
[解决办法]
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt");
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字
system("dir /b /ad  c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。

[解决办法]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
    char a[256];
    char cmd[512];
    FILE *f;
 
    printf("Enter a path for list all files:\n");
    fgets(a,256,stdin);
    if ('\n'==a[strlen(a)-1]) a[strlen(a)-1]=0;
    if ('\\'!=a[strlen(a)-1]) strcat(a,"\");
    sprintf(cmd,"dir "%s*.*" /b /s /a-d >d:\\allfiles.txt",a);
    system(cmd);
    f = fopen("d:\\allfiles.txt", "r");
    if (NULL==f) return;
    while (1) {
        if (NULL==fgets(cmd,512,f)) break;
        printf("%s",cmd);
    }
    fclose(f);
    return;
}

热点排行