当前位置:首页 » 编程语言 » 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