2013-10-05 55 views
0

我假設文件夾「my」中有兩個文本文件abc.txtdef.txt。我有一個程序直接進入該文件夾並搜索特定文件,如果該特定文件發現,然後如何訪問該文件的信息。如何通過c文件處理訪問文件夾中的特定文件

我知道如何通過文件處理讀取C文件中的寫入文件,但我不知道如何搜索特定文件,之後讀取特定文件以匹配文件中的特定字符串。

**All these things access through file handling in C.**

所以,請,如果任何人有任何解決方案,我將感謝的是

例子進行了解最好的方式。

在此先感謝

+0

如果你知道如何讀取文件,和C有一個像讓你搜索字符串中的東西'的strstr()'功能,你需要知道把它們放在一起是什麼? – Barmar

+0

你在使用什麼平臺? Linux呢?蘋果系統?目錄遍歷和列表很不幸取決於平臺,所以它很重要。 – Nikhil

+0

POSIX:['#include '](http://pubs.opengroup.org/onlinepubs/007908799/xsh/dirent.h.html) – mouviciel

回答

1

要獲得文件的列表中的目錄在Linux中,你可以使用「執行opendir」,「READDIR」,並從「dirent.h」 closedir「功能。例如:

#include <dirent.h> 
#include <stdio.h> 

int ListDir(const char *pDirName) 
{ 
    DIR *pDir; 
    struct dirent *pEntry; 

    pDir = opendir(pDirName); 
    if (!pDir) 
    { 
      perror("opendir"); 
      return -1; 
    } 

    while ((pEntry = readdir(pDir)) != NULL) 
    { 
      printf("%s\n", pEntry->d_name); 
    } 

    closedir(pDir); 
    return 0; 
} 
相關問題