请输入您要查询的百科知识:

 

词条 findfirst, findnext
释义

函数名: findfirst, findnext

功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件

用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);

int findnext(struct ffblk *ffblk);

程序例:

/* findnext example */

#include <stdio.h>

#include <dir.h>

//两个函数需要定义一个结构体来存储函数返回的数据。结构体如下:

struct ffblk

{

char ff_reserved[21]; /*DOS保留字*/

char ff_attrib; /*文件属性*/

int ff_ftime; /*文件时间*/

int ff_fdate; /*文件日期*/

long ff_fsize; /*文件长度*/

char ff_name[13]; /*文件名*/

}

//将结构体中的ff_name[13]显示出来即可。

int main(void)

{

struct ffblk ffblk;

int done;

printf("Directory listing of *.*\");

done = findfirst("*.*",&ffblk,0);

while (!done)

{

printf(" %s\", ffblk.ff_name);

done = findnext(&ffblk);

}

return 0;

}

用于文件的查找和删除等等。

程序例:显示所有的文件:

#include <io.h>

#include <iostream>

#include <string>

#include <windows.h>

using namespace std;

string sRoot = "D:\\\\";

string sSuffix = "\\\\*.*"; // 后缀

void Move(string sPath);

void main()

{

Move(sRoot);

system("pause");

}

void Move(string sPath)

{

struct _finddata_t file;

long hFile, hFileNext;

string sPathLast = sPath + sSuffix; // sPathLast = "c:\\test\\*.*"

hFile = _findfirst(sPathLast.c_str(), &file);

if(hFile == -1)

{

cout<<"文件不存在."<<endl;

return;

}

else

{

cout<<file . name<<endl;

}

hFileNext = _findnext(hFile, &file);

while(_findnext(hFile, &file) == 0)

{

if(file.attrib == _A_SUBDIR)

{

string sAddPath = sPath;

sAddPath += "\\\\";

sAddPath += file . name;

cout<<"目录:"<<sAddPath<<endl;

Move(sAddPath);

}

else

{

string sAddPath = sPath;

sAddPath += "\\\\";

sAddPath += file . name;

cout<<"文件:"<<sAddPath<<endl;

}

}

}

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/1 13:48:00