C语言中,如何读取同一文件夹下的所有txt文件?

C语言中,如何读取同一文件夹下的所有txt文件?,第1张

#include <stdio.h>

#include<io.h>

int main(int argc, char *argv[])

{

struct _finddata_t fa

long fHandle

if( (fHandle=_findfirst( "*.txt", &fa ))==-1L )//这老派里可以改成需要的目录

{

printf( "当前目录下没有txt文件裤厅\n")

return 0

}

else

do

{

printf( "胡含隐找到文件:%s\n", fa.name )

}while( _findnext(fHandle,&fa)==0 )

_findclose( fHandle )

return 0

}

//自己写的代码,绝对可用,望采纳

#include <string> // std::string

#include <vector> // std::vector

#include <io.h>// _finddata_t

#include <iostream>// std::cout

//dirpath为你要查找的文件件绝对路径,如txt在D盘文件扒歼夹名为1的情况下,即输入:

//std::string = "D:\\1\\"

//std::vector<std::string>filepaths//为所有高瞎txt的文件名称,这是输出参数

//std::string regular_expression_input = "*.txt"

//你写一个主函数即可使用。

int get_filenames_under_dir_based_on_regular_expression(const std::string&dirpath, std::vector<std::string>&filepaths, const std::string&regular_expression_input)

{

struct _finddata_t filefind

std::string curr = dirpath + regular_expression_input

int done=0,i,handle

int N = 0

std::string tmp_string

if( (handle = _findfirst(curr.c_str(),&filefind)) == -1 )

{

return N

}

else

{

if( strcmp(filefind.name,"..") &&strcmp(filefind.name,"."戚此空) )

{

filepaths.push_back(filefind.name)

N++

}

}

while( !(done = _findnext(handle,&filefind)) )

{

if((!strcmp(filefind.name,"..")) || (!strcmp(filefind.name,".")))

{

continue

}

if( !(filefind.attrib == _A_SUBDIR) )

{

filepaths.push_back(filefind.name)

N++

}

}

_findclose(handle)

return N

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12213983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存