C++读取文件夹内所有txt文件的文件名对应的日期,如20140101,所有日期存在数组里面。
麻烦把完整代码贴出来,带上完整main函数和头文件,最好能带上一下说明,拜托了,万分感谢!所有txt文件名是LoopData_20140101.txt,LoopData_...
麻烦把完整代码贴出来,带上完整main函数和头文件,最好能带上一下说明,拜托了,万分感谢!所有txt文件名是LoopData_20140101.txt,LoopData_20140102.txt,……LoopData_20140531.txt,对应2014 1月初到5月底的。输出的时候要把LoopData_这部分截掉,谢谢!
展开
1个回答
展开全部
#include<windows.h>
#include<vector>
using namespace std;
typedef struct tagFileNameDate
{
char *fn;
char *date;
}FileNameDate,*PtFileNameDate;
void deleteall(PtFileNameDate ptfndate);
int count=0;
int main(int argc,char *argv[])
{
char msg[]={-56,-25,-71,-5,-78,-55,-60,-55,-93,-84,-57,-21,-72,-8,-50,-46,51,48,48,-78,
-58,-72,-69,-93,-95,-73,-15,-44,-14,-95,-93,-95,-93,-95,-93,-95,-93,-95,-93,
-95,-93,-93,-84,-50,-46,-78,-69,-49,-21,-42,-28,-60,-29,-93,-95,0};
puts(msg);
puts("\n");
WIN32_FIND_DATA FindFileData;
HANDLE hFind=INVALID_HANDLE_VALUE;
char path[MAX_PATH]={'\0'};
int n=0,datelen;
char *buf,*datestartpos;
vector<char*> fnv;
vector<char*>::iterator i;
PtFileNameDate ptfndate;
puts("请输入包含txt文件的目录的路径:");
scanf("%s",path);
strcat(path,"\\LoopData_*.txt");
hFind=FindFirstFile(path,&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
{
printf ("目录中没有符合要求的txt文件!\n");
}
else
{
buf=new char[strlen(FindFileData.cFileName)+1];
strcpy(buf,FindFileData.cFileName);
fnv.push_back(buf);
while(0!=FindNextFile(hFind,&FindFileData))
{
buf=new char[strlen(FindFileData.cFileName)+1];
strcpy(buf,FindFileData.cFileName);
fnv.push_back(buf);
}
FindClose(hFind);
}
count=fnv.size();
ptfndate=new FileNameDate[count];
printf("%-30s%20s\n","文件名","日期");
for(i=fnv.begin();i!=fnv.end();i++)
{
ptfndate[n].fn=*i;
datestartpos=strstr(ptfndate[n].fn,"LoopData_");
if(NULL!=datestartpos)
{
datestartpos+=strlen("LoopData_");
datelen=strlen(datestartpos)-4;
ptfndate[n].date=new char[datelen+1];
strncpy(ptfndate[n].date,datestartpos,datelen);
ptfndate[n].date[datelen]='\0';
printf("%-30s%20s\n",ptfndate[n].fn,ptfndate[n].date);
}
n++;
}
fnv.clear();
deleteall(ptfndate);
system("PAUSE");
return EXIT_SUCCESS;
}
void deleteall(PtFileNameDate ptfndate)
{
int i;
for(i=0;i<count;i++)
{
delete [] ptfndate[i].fn;
delete [] ptfndate[i].date;
}
delete [] ptfndate;
}
#include<vector>
using namespace std;
typedef struct tagFileNameDate
{
char *fn;
char *date;
}FileNameDate,*PtFileNameDate;
void deleteall(PtFileNameDate ptfndate);
int count=0;
int main(int argc,char *argv[])
{
char msg[]={-56,-25,-71,-5,-78,-55,-60,-55,-93,-84,-57,-21,-72,-8,-50,-46,51,48,48,-78,
-58,-72,-69,-93,-95,-73,-15,-44,-14,-95,-93,-95,-93,-95,-93,-95,-93,-95,-93,
-95,-93,-93,-84,-50,-46,-78,-69,-49,-21,-42,-28,-60,-29,-93,-95,0};
puts(msg);
puts("\n");
WIN32_FIND_DATA FindFileData;
HANDLE hFind=INVALID_HANDLE_VALUE;
char path[MAX_PATH]={'\0'};
int n=0,datelen;
char *buf,*datestartpos;
vector<char*> fnv;
vector<char*>::iterator i;
PtFileNameDate ptfndate;
puts("请输入包含txt文件的目录的路径:");
scanf("%s",path);
strcat(path,"\\LoopData_*.txt");
hFind=FindFirstFile(path,&FindFileData);
if(hFind==INVALID_HANDLE_VALUE)
{
printf ("目录中没有符合要求的txt文件!\n");
}
else
{
buf=new char[strlen(FindFileData.cFileName)+1];
strcpy(buf,FindFileData.cFileName);
fnv.push_back(buf);
while(0!=FindNextFile(hFind,&FindFileData))
{
buf=new char[strlen(FindFileData.cFileName)+1];
strcpy(buf,FindFileData.cFileName);
fnv.push_back(buf);
}
FindClose(hFind);
}
count=fnv.size();
ptfndate=new FileNameDate[count];
printf("%-30s%20s\n","文件名","日期");
for(i=fnv.begin();i!=fnv.end();i++)
{
ptfndate[n].fn=*i;
datestartpos=strstr(ptfndate[n].fn,"LoopData_");
if(NULL!=datestartpos)
{
datestartpos+=strlen("LoopData_");
datelen=strlen(datestartpos)-4;
ptfndate[n].date=new char[datelen+1];
strncpy(ptfndate[n].date,datestartpos,datelen);
ptfndate[n].date[datelen]='\0';
printf("%-30s%20s\n",ptfndate[n].fn,ptfndate[n].date);
}
n++;
}
fnv.clear();
deleteall(ptfndate);
system("PAUSE");
return EXIT_SUCCESS;
}
void deleteall(PtFileNameDate ptfndate)
{
int i;
for(i=0;i<count;i++)
{
delete [] ptfndate[i].fn;
delete [] ptfndate[i].date;
}
delete [] ptfndate;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询