當前位置:首頁 » 編程語言 » c語言讀取目錄

c語言讀取目錄

發布時間: 2024-06-17 10:28:38

『壹』 在windows下 怎麼用c語言遍歷文件夾要用純c的

什麼叫純C?
用C語言遍歷文件肯定需要用到函數,標准C下貌似沒有這個函數,但是使用VC的函數庫可能可以實現,如果實在不行可以用第三方函數庫,,,還不行的話用system("command");引用dos命令可以遍歷,

『貳』 C語言編程:讀取當前目錄下的英文文章file.txt,按原格式顯示在屏幕上,咋寫

#include <stdio.h>

#include<stdlib.h>

#include<string.h>

#define N 10000

int main ()

{

char str[N];

char a[111][30];

int k=0,j=0;

FILE *fp;

if((fp=fopen("D:\file.txt","r"))!=NULL)

printf("文件打開成功 ");

else

{

printf("文件打開失敗 ");

exit(0);

}

while(fgets(str,N,fp)!=NULL)//利用fgets函數將文本中的非空字元全部儲存在數組str中

for(int i=0;i<strlen(str);i++)

if(str[i]!=' '&&str[i]!='.')

a[k][j++]=str[i];//將每個單詞儲存在二維數組a的每一行

else

{

k++;//行下標

j=0;//列下標

}

printf("共%d個單詞 ",k);

for(int i=0;i<=k;i++)

puts(a[i]);

fclose(fp);//關閉文件

return 0;

}

『叄』 C璇璦濡備綍閬嶅巻鐩褰 錛圕++涔熷彲浠ワ級 findfirst findnext鎬庝箞鐢錛

#include <windows.h>
#include <stdio.h>
FILE *fp;
void findFile(char filePath[])//榪欎釜鏄浣犺佺殑鍑芥暟
{
char szFind[MAX_PATH];//榪欐槸瑕佹壘鐨
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char szFile[MAX_PATH];

strcpy(szFind,filePath);
strcat(szFind,"\\*.*");//鍒╃敤閫氶厤絎︽壘榪欎釜鐩褰曚笅鐨勬墍浠ユ枃浠訛紝鍖呮嫭鐩褰

hFind=FindFirstFile(szFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind) return;

while(TRUE)
{

if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//榪欐槸鐩褰
{
if(FindFileData.cFileName[0]!='.')//.琛ㄧず褰撳墠鐩褰曪紝鍥犱負姣忎釜鐩褰曚笅闈㈤兘鏈変袱涓榛樿ょ洰褰曞氨鏄..鍜.鍒嗗埆琛ㄧず涓婁竴綰х洰褰曞拰褰撳墠鐩褰
{
strcpy(szFile,filePath);
strcat(szFile,"\\");
strcat(szFile,FindFileData.cFileName);
findFile(szFile);//瀵繪壘榪欎釜鐩褰曚笅闈㈢殑鏂囦歡
}
}
else
{
fprintf(stdout,"%s\\%s\n",filePath,FindFileData.cFileName);//鎵撳嵃鍑虹洰褰曚笅鐨勬枃浠剁殑璺寰勫拰鍚嶇О
fprintf(fp,"%s\\%s\n",filePath,FindFileData.cFileName);//榪欏皢緇撴灉瀛樻。鍒癱:\\path.txt涓銆
}
if(!FindNextFile(hFind,&FindFileData))//瀵繪壘涓嬩竴涓鏂囦歡
break;
}
FindClose(hFind);//鍏抽棴鍙ユ焺
}

int main()
{
fp = fopen("C:\\path.txt","w");
findFile("D:\\e-book\\瀹炰範\\闅忕瑪\\璇諱功ing");//榪欓噷鏄浣犺侀亶鍘嗙殑鐩褰曪紝浣犺嚜宸卞彲浠ユ敼鍙,瀹冧細鏄劇ず榪欎釜鐩褰曚笅鐨勬墍鏈夋枃浠訛紝鍖呮嫭榪欎釜鐩褰曚笅瀛愮洰褰曚笅鐨勬枃浠躲
fclose(fp);
return 0;
}
紼嬪簭濡備笂錛屾槸鎶婄粨鏋滆緭鍑哄埌鏍囧噯杈撳嚭涓婏紝騫朵笖瀛樻。鍒癈:\\path.txt涓銆
鍙浠ヨ繍琛岀殑錛屾垜宸茬粡嫻嬭瘯榪囥
宸ュ叿鏄痸c6.0.

『肆』 怎樣使用C語言列出某個目錄下的文件

C語言本身沒有提供象dir_list()這樣的函數來列出某個目錄下所有的文件。不過,利用C語言的幾個目錄函數,你可以自己編寫一個dir_list()函數。 首先,頭文件dos.h定義了一個find_t結構,它可以描述DOS下的文件信息,包括文件名、時間、日期、大小和屬性。其次,C編譯程序庫中有_dos_findfirst()和_dos_findnext()這樣兩個函數,利用它們可以找到某個目錄下符合查找要求的第一個或下一個文件。 dos_findfirst()函數有三個參數,第一個參數指明要查找的文件名,例如你可以用「*.*」指明要查找某個目錄下的所有文件。第二個參數指明要查找的文件屬性,例如你可以指明只查找隱含文件或子目錄。第三個參數是指向一個find_t變數的指針,查找到的文件的有關信息將存放到該變數中。 dos_findnext()函數在相應的目錄中繼續查找由_dos_findfirst()函數的第一個參數指明的文件。_dos_findnext()函數只有一個參數,它同樣是指向一個find_t變數的指針,查找到剛文件的有關信息同樣將存放到該變數中。 利用上述兩個函數和find_t結構,你就可以遍歷磁碟上的某個目錄,並列出該目錄下所有的文件,請看下例: #include <stdio.h> #include <direct.h> #include <dos.h> #include <malloc.h> #include <memory.h> #include <string.h> typedef struct find_t FILE_BLOCK void main(void); void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * / int ret_code; / * Define a variable to store the return codes * / / * Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. * / ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block); /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. * / while (ret_code == 0){/* Print the file's name * / printf(" %-12s\n, f_block, name); / * Use the -dos_findnext() function to look

『伍』 linux c 查看當前目錄下是否有指定文件

1. Shell 版本
#獲取當前腳本所在絕對路徑
cur_dir=$(cd "$(dirname "$0")"; pwd)

2. C語言版本
方法一、用realpath函數。這種方法用於開機啟動程序獲取自身目錄會出錯
char current_absolute_path[MAX_SIZE];
//獲取當前目錄絕對路徑
if (NULL == realpath("./", current_absolute_path))
{
printf("***Error***\n");
exit(-1);
}
strcat(current_absolute_path, "/");
printf("current absolute path:%s\n", current_absolute_path);
方法二、用getcwd函數。這種方法用於開機啟動程序獲取自身目錄會出錯
char current_absolute_path[MAX_SIZE];
//獲取當前目錄絕對路徑
if (NULL == getcwd(current_absolute_path, MAX_SIZE))
{
printf("***Error***\n");
exit(-1);
}
printf("current absolute path:%s\n", current_absolute_path);

方法三、用readlink函數。這種方法最可靠,可用於開機啟動程序獲取自身目錄
char current_absolute_path[MAX_SIZE];
//獲取當前程序絕對路徑
int cnt = readlink("/proc/self/exe", current_absolute_path, MAX_SIZE);
if (cnt < 0 || cnt >= MAX_SIZE)
{
printf("***Error***\n");
exit(-1);
}
//獲取當前目錄絕對路徑,即去掉程序名
int i;
for (i = cnt; i >=0; --i)
{
if (current_absolute_path[i] == '/')
{
current_absolute_path[i+1] = '\0';
break;
}
}
printf("current absolute path:%s\n", current_absolute_path);

『陸』 c語言如何獲取用戶通過鍵盤輸入的文件目錄中的文件名和文件路徑,ballball大佬幫幫我🙏求代碼

int main()
{
string s = "c:\\abc\\def\\text.txt";
int xie_index = s.find_last_of('\\'); // 路徑中最後一個\的位置
string file_dirname = s.substr(0, xie_index + 1);
string file_basename = s.substr(xie_index + 1, s.size());
cout << file_dirname << endl << file_basename << endl;
}

熱點內容
睿教育密碼是多少位數 發布:2024-06-26 15:38:50 瀏覽:185
androidsqlite條件查詢 發布:2024-06-26 15:37:36 瀏覽:616
淘寶上傳圖片文件夾 發布:2024-06-26 15:19:21 瀏覽:685
需要配置認證伺服器地址 發布:2024-06-26 15:00:49 瀏覽:240
c語言鏈接錯誤 發布:2024-06-26 15:00:39 瀏覽:244
安卓相冊在哪裡設置 發布:2024-06-26 15:00:38 瀏覽:471
dhcp伺服器如何固定ip地址 發布:2024-06-26 15:00:38 瀏覽:433
Python數據分析實例 發布:2024-06-26 14:32:31 瀏覽:27
尚學堂java培訓課程 發布:2024-06-26 14:19:45 瀏覽:472
樂視視頻手機緩存 發布:2024-06-26 13:49:12 瀏覽:378