【問題】如何讀取目錄?


Recommended Posts

你要不要用系統提供的指令

然後再把指令傳回的資料讀進程式?

比如說unix的ls、ll...

dos的dir

用法是這樣:

#include<stdio.h>

FILE *xxx=popen("指令","r");

上面r也可以填r+

其他的都不要用

填w跟w+會直接把資料顯示出來

a跟a+一讀就當...

其他地方就跟一般檔案使用一樣

最後再pclose(FILE *);就好了

如果你輸入的指令要求使用者額外輸入東西

如砍檔案時可能問你yes or no?

這種似乎沒辦法讓程式代打

我還沒辦法解決

還有feof(FILE *);也可以用

讀到結尾值為真

至於讀進去以後要怎麼把你要的資料抽出來...

這您就加油吧

鏈接文章
分享到其他網站

喔喔那是我沒講清楚

1. 引入stdio.h,是C的函式(C++裡面用啥我不知道)

2. popen是用於使用系統提供的指令

用法像這樣

FILE *xxx=popen("dir","r");

就好像你平常在命令提示字元裡面打dir會出現這些東西

 磁碟區 C 中的磁碟沒有標籤。
磁碟區序號: F444-985D

C:\Documents and Settings\A人 的目錄

2004/12/21 下午 02:48 <DIR> .
2004/12/21 下午 02:48 <DIR> ..
2006/02/04 下午 08:29 600 PUTTY.RND
2004/01/16 上午 02:30 <DIR> WINDOWS
2006/02/07 上午 02:47 <DIR> 甲片
2004/01/16 上午 02:18 <DIR> 「開始」功能表
2005/05/31 上午 09:20 <DIR> Favorites
2006/02/07 下午 03:59 6,553,600 ntuser.dat
2 個檔案 6,554,200 位元組
6 個目錄 1,503,043,584 位元組可用

所以從xxx裡面就會讀到上面這些東西

讀取一樣是用fgets、fgetc、fscanf等這些指令

喔喔還有我們同年

鏈接文章
分享到其他網站

這是我寫的讀取目錄下檔案的程式

下面是c語言程式,不是php

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *ftr=popen("dir","r");
char temp,data[100][100],output[100][51];
int i=0,j=0;
while(!feof(ftr))
{
temp=fgetc(ftr);
if(temp=='\n')
{
data[i++][j]='\0';
j=0;
}
else
{
data[i][j++]=temp;
}
}
for(j=5;j<i-2;j++)
sscanf(data[j],"%*s %*s %*s %*s %s",output[j]);
for(j=0;j<i-2;j++)
printf("%s\n",output[j]);
fclose(ftr);
system("pause");
return 0;
}

註解有空我在打,不會很難懂的

這程式讀取目錄下的東西必須90個以下(可以修正)

檔名不能超過25個中文字(可以修正)

檔名不能有空白(不能修正)

話說我不小心按到噓文了,對不起

怎樣去掉噓文?

鏈接文章
分享到其他網站


#include<stdio.h>
#include<dirent.h>
int main(){
DIR *a=opendir(".");
while(readdir(a))
{
printf("%s\n",a->dd_dir.d_name);
}
closedir(a);
return 0;
}

太晚講...

好吧

其實這才是你一開始要找的函式

引入dirent.h

詳細說明有空在PO

不好意思m(_ _)m

糟糕用到我妹的帳號...

我是ckguozhi

鏈接文章
分享到其他網站

/*

* DIRENT.H (formerly DIRLIB.H)

* This file has no copyright assigned and is placed in the Public Domain.

* This file is a part of the mingw-runtime package.

* No warranty is given; refer to the file DISCLAIMER within the package.

*

*/

沒版權的應該是?

雖然FreeBSD裡面也有這個檔案

但是定義似乎有點不太一樣

大哥你還是用popen的辦法好了

--

壇主?

鏈接文章
分享到其他網站
最初由 ckguozhi 發表

/*

* DIRENT.H (formerly DIRLIB.H)

* This file has no copyright assigned and is placed in the Public Domain.

* This file is a part of the mingw-runtime package.

* No warranty is given; refer to.............(論壇訊息:引文過長 恕刪)

不不不

popen似乎不是常用函式庫

有些編譯器不支援

So....

之前改成 dir /o:n >> log.txt

然後用 fopen 讀檔

成功後再將 log.txt 刪除

不過既然你已經說了

就根本不用寫程式啦

直接 dir /b /o:n >> log.txt

就是我們要的東西啦

鏈接文章
分享到其他網站
  • 2 weeks later...

寫出來了

不過仍然是用 DIR


#include <stdio.h>
#include <stdlib.h>

// strtok(), strtok_r()

int main()
{
FILE *cmd;
FILE *pt;
char **in,tmp;
char *ch_pt[100]; // for strtok()
char str[260]; // for strtok()
char str2[260];
char *str_chk;
int c_num=10,cnt=0;
int a,b,c,d;

in = (char**)malloc( sizeof(char*) * c_num );
for(a=0;a<c_num;a++) in[a]=(char*)malloc( sizeof(char) * 260 );
//---------------------------------------------
system("dir /o:n >> log.tmp");
cmd = fopen("log.tmp","r");
a=0;
while( !feof(cmd) )
{
tmp = fgetc(cmd);
if( tmp=='\n' )
{
in[cnt][a]='\0';
if( cnt+1 >= c_num )
{
in = (char**)realloc(in,sizeof(char*) * (c_num+10));
for(c=c_num;c<c_num+10;c++) in[c]=(char*)malloc( sizeof(char) * 260 );
c_num+=10;
}
cnt++;
a=0; // in[cnt][a]
}else{
in[cnt][a]=tmp;
a++;
}
}
fclose(cmd);
remove("log.tmp");
for(a=0;a<cnt;a++)
printf("%s\n",in[a]);
/**********************************************
規則:
1st token 一定有 / 斜線
3rd token 一定有冒號 (時間)

4th token 有 <DIR> 表示是資料夾
5th token 以後(包括 5th token)的都是檔案名稱
**********************************************/
pt = fopen("result.txt","w");
a=0;
while(a<cnt)
{
//printf("%s\n",in[a]);
sprintf(str,"%s",in[a]);
//printf("%s\n",str);
ch_pt[0] = strtok(str," ");
if( ch_pt[0] != NULL )
str_chk = strstr(ch_pt[0],"/");
if(str_chk!=NULL)
{
for(b=1;b<100;b++)
{
ch_pt[b]=strtok(NULL," ");
if( ch_pt[b] == NULL ){ break; }
}
str_chk = strpbrk(ch_pt[3],"DIR"); //
if( str_chk == NULL )
{
fprintf(pt,"%s",ch_pt[4]);
for(c=5;c<100;c++)
{
if(ch_pt[c]==NULL) break;
fprintf(pt," %s",ch_pt[c]);
}
fprintf(pt,"\n");
}
}
a++;
for(b=0;b<100;b++)
ch_pt[b] = NULL;
for(b=0;b<260;b++)
str[b] = '\0';
}
fclose(pt);
//---------------------------------------------
free(in);
//system("pause");
return 0;
}

鏈接文章
分享到其他網站

請登入後來留意見

在登入之後,您才能留意見



立即登入