现在的位置: 首页 > 综合 > 正文

网易博客转载UNIX C学习(3部分)

2013年11月06日 ⁄ 综合 ⁄ 共 5545字 ⁄ 字号 评论关闭

1.使用_findfirst(CRT)或者FindFirstFile(API)实现循环打印指定目录的所有子目录及文件(dir/s,实现在子目录中查找指定的文件,如果不同目录下有同名文件,都列出来(dir/s[filename].提示:在MSDN中使用搜索功能查找例程。

答:

#include <iostream>

#include <io.h>

#include <windows.h>

#include <direct.h>

using namespace std;

 

void search_directory(char *ps)//注意:不能隐藏文件扩展名。这个函数是在一个目录下面寻找指定文件的位置

{

      struct _finddata_t filestruct;

   long handle;

      char path_search[MAX_PATH];

      handle=_findfirst("*",&filestruct);

      if(handle==-1)

      {cout<<"findfirst() fail!";return;}

      if(GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)

             if(filestruct.name[0]!='.')//是目录

             {_chdir(filestruct.name);search_directory(filestruct.name);_chdir("..");}

             else //不是目录,是具体的

             {

                    if (!strcmp(filestruct.name,ps) )

                    {_getcwd(path_search,MAX_PATH);strcat(path_search,"\\");

                    strcat(path_search,filestruct.name);

                    cout<<path_search<<endl;}

             }

             while(!_findnext(handle,&filestruct))//找下一个

             {

                    if (GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)

                    {

                           if(*filestruct.name!='.')//是目录

                           {

                                  _chdir(filestruct.name);

                                  search_directory(ps);

                                  _chdir("..");

                           }

                    }

                    else

                    {

                           if(! strcmp(filestruct.name,ps))

                           {

                                  _getcwd(path_search,MAX_PATH);

                                  strcat(path_search,"\\");

                                  strcat(path_search,filestruct.name);

                                  cout<<path_search<<endl;

                           }

                    }

             }

}

 

void print_directory(char *ps)//打印指定下面所有子目录和文件

{

      struct _finddata_t filestruct;

   long handle;//char *p="目录:";

      char path_search[MAX_PATH];

      handle=_findfirst("*",&filestruct);

      if(handle==-1)

      {cout<<"findfirst() fail!";return;}

      if(GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)

             if(filestruct.name[0]!='.')//!='.'是目录

             {cout<<filestruct.name<<"目录下的文件有:"<<endl;;

             //strcat(path_search,filestruct.name);

             //cout<<filestruct.name<<endl;

             _chdir(filestruct.name);print_directory(filestruct.name);_chdir("..");

             cout<<"..........................................................."<<endl;}

             else //不是目录,是具体的

             {

                    //if (!strcmp(filestruct.name,ps) )

                    cout<<"非目录文件:"<<endl;;

                    {_getcwd(path_search,MAX_PATH);strcat(path_search,"\\");

                    strcat(path_search,filestruct.name);

                    cout<<path_search<<endl;}

             }

             while(!_findnext(handle,&filestruct))//找下一个

             {

                    if (GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)

                    {

                           if(*filestruct.name!='.')//是目录

                           {

                                  cout<<filestruct.name<<"目录下的文件有:"<<endl;

                                 
//strcat(path_search,filestruct.name);

                                  

//                              
strcpy(path_search,p);

                                 //strcat(path_search,filestruct.name);

//         
            cout<<filestruct.name<<endl;

                                 _chdir(filestruct.name);

                                 print_directory(filestruct.name);

                                 _chdir("..");

                                  cout<<"..........................................................."<<endl;

                           }

                    }

                    else

                    {

                           //if(! strcmp(filestruct.name,ps))

                           {  
cout<<"
非目录文件:"<<endl;;;

                                  _getcwd(path_search,MAX_PATH);

                                  strcat(path_search,"\\");

                                  strcat(path_search,filestruct.name);

                                  cout<<path_search<<endl;

                           }

                    }

             }

}

void main()

{

      struct _finddata_t c_file;

   long handl;

      char *p=new 
char[20];

      cin>>p;

      print_directory(p);

}

或者用CFileFind或者函数FindFirstFile来实现。

2.开发一个输出流类COutput,支持对基本类型的输入输出,内部使用printf根据,重载输出<<函数的参数,按类型输出。

答:

#define endl "endl"

//输出流类COutput,支持对基本类型的输入输出,内部使用printf根据

class COutput

{

public:

        
COutput &operator<<(const char*ps){if(!strcmp(ps,"endl")) printf("\n");else printf("%s",ps);return *this;}

             COutput &operator<<(char c){printf("%c",c);return *this;}

            COutput &operator<<(short i) {printf("%sd",i);return *this;}

            COutput &operator<<(int i){printf("%d",i);return *this;}

            COutput &operator<<(long l){printf("%ld",l);return *this;}

            COutput &operator<<(double d){printf("%f",d);return *this;}

             COutput &operator<<(const void* a){printf("%d",a);return *this;}

};

 

class CInput

{

public:

      CInput &operator>>(const char*ps){scanf("%s",ps);return *this;}

     CInput &operator>>(char c){scanf("%c",c);return *this;}

     CInput &operator>>(short i) {scanf("%sd",i);return *this;}

     CInput &operator>>(int i){scanf("%d",i);return *this;}

     CInput &operator>>(long l){scanf("%ld",l);return *this;}

     CInput &operator>>(double d){scanf("%f",d);return *this;}

     CInput &operator>>(const void* a){scanf("%d",a);return *this;}

};

抱歉!评论已关闭.