CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C++ Builder >  基础类

文件列表的问题

楼主tjzzx888(青草)2002-12-11 11:33:37 在 C++ Builder / 基础类 提问

我想把目录树保存在一个.txt文件里,编程如下:  
   
          void   __fastcall   TForm1::Button1Click(TObject   *Sender)  
  {  
      TSearchRec   sr;  
      int   iAttributes   =   0;  
      int   iHandle=0;  
      iHandle=FileCreate("e:\\test.txt");  
      AnsiString   content;  
      if   (FindFirst(Edit1->Text,   iAttributes,   sr)   ==   0)  
   
      {  
          do  
          {  
              if   (sr.Attr   ==   faDirectory)  
              {  
                    content=sr.Name;  
                    content=content+"(文件夹)\n";  
                    FileWrite(iHandle,content.c_str(),strlen(content.c_str()));  
                    Label1->Caption=sr.Name;  
              }  
              else  
              {  
                    content=sr.Name+"\n";  
                    FileWrite(iHandle,content.c_str(),strlen(content.c_str()));  
                    Label1->Caption=content;  
              }  
          }   while   (FindNext(sr)   ==   0);  
          FindClose(sr);  
      }  
      FileClose(iHandle);  
  }  
   
      程序能编译,也能执行,可是test.txt文件中却是空的,为什么?谢谢 问题点数:20、回复次数:6Top

1 楼hatumei(明月)回复于 2002-12-11 11:51:56 得分 0

用文件流  
  #include   <fstream.h>  
  .......  
  ofstream   os(("e:\\test.txt");  
  os<<第一行<<endl;  
  os<<第二行<<endl;  
  os.close();Top

2 楼invalid(空心菜(Python是个好东东,大家多用用!))回复于 2002-12-11 11:55:58 得分 5

自己单步跟踪看看啊!Top

3 楼tjzzx888(青草)回复于 2002-12-11 14:44:49 得分 0

to   invalid(空心菜之2.0开发中)   :  
        单步跟踪,  
        我测试的目录下有一TXT文件(use.cpp),一个文件夹(lx文件夹),  
        但do   只做一次,  
        test.txt中内空是use.cpp  
        lx文件夹怎么没查到呢?  
   
  注:Edit1->Text="e:\test\*.*Top

4 楼hatumei(明月)回复于 2002-12-12 09:04:30 得分 10

改为:  
  int   iAttributes   =   63;Top

5 楼ly_liyong(编程浪子)回复于 2002-12-12 09:43:39 得分 5

String   TForm1::ctrl_SaveFileList(String   dir)  
  {  
          //   the   section   detect   if   the   file   "xml"   exist,   terminate   if   not   exist  
                  if   (!(FileExists("FileList.xml")))  
          {  
                  ::ShowMessage("File   dose   not   exist");  
                  returnvalue   =   -3;  
                  Application->Terminate();  
          }  
          //   the   "xml"   exist   ,application   gose   on  
          else  
          {  
          try{  
                  xmlInterface->LoadFromFile(L"FileList.xml");  
                  xmlInterface->Active   =   true;  
                  nodelist   =   xmlInterface->ChildNodes;  
                  node   =   xmlInterface->ChildNodes->FindNode("FileList");  
   
                  FindFirst(dir   +   "\\*.*",   faAnyFile,   SearchRec);  
                  FindNext(SearchRec);  
            while(FindNext(SearchRec)   ==   0)  
                  {  
                            node->AddChild(String(SearchRec.Name));//,String(SearchRec.Ne));  
  //here   invode   the   function   to   handle   the   folder   of   faDirectory   property  
                            ctrl_GetChildFile(dir,node,nodelist,SearchRec);  
  //   If   current   Dir   include   folders.   the   code   below   check   the   folder   and  
  //   the   files   in   it   with   which   will   be   write   to   realist   in   XML   file.  
                  }  
                  }  
          //catch   exceptions   and   process   it  
          catch(...)  
          {  
                  ::ShowMessage("Cannot   finish   write   FileList.xml!");  
                  returnvalue   =   -4;  
                  Application->Terminate();  
          }  
          //   Release   the   resources   used   above  
                  xmlInterface->SaveToFile();  
                  xmlInterface->Active   =   false;  
                  FindClose(SearchRec);  
                  ::ShowMessage("File   saved   in   the   FileList.xml   successfuly");  
          }  
   
  }  
   
  //---------------------------------------------------------------------------  
  void   TForm1::ctrl_GetChildFile(String   dir2,IXMLNode   *node1,IXMLNodeList   *nodelist1,TSearchRec   SearchRec1)  
  {  
    //this   function   handle   the   files   in     subdirectory   .complete   the   scaning   of   the   file   in   folders.  
    //using   the   递归   method   to   scaning   all   folders   in   a   dir   or   subdirectories.  
    //   all   the   varities   below   can   not   be   globe   vars   coz   递归   method   use   the   vars   to   alocate   the  
    //   nodes   and   folders   realtime                        
                          if(SearchRec.Attr   &   faDirectory)  
                          {  
                          TSearchRec   SearchRec2;  
                          ::ShowMessage("Folder   Find");  
                          nodelist1   =   node1->GetChildNodes();  
                          node1   =   nodelist1->FindNode(SearchRec1.Name);  
                          dir2   =   dir2   +   "\\"   +   SearchRec1.Name;  
                          SearchRec1   =   SearchRec2;  
                          FindFirst(dir2   +"\\*.*",   faAnyFile,   SearchRec1);  
                          FindNext(SearchRec1);  
  //   write   file   name   in   folders   to   the   sub   level   of   xml   docment   list  
                                                  while(FindNext(SearchRec1)   ==   0)  
                                                          {  
                                                          node1->AddChild(String(SearchRec1.Name));  
  //   here   invoke   the     right   self   function   to   complement   递归  
                                                          ctrl_GetChildFile(dir2,node1,nodelist1,SearchRec1);  
                                                          ::ShowMessage("   folder");  
                                                          }  
                          FindClose(SearchRec1)   ;          
   
                          }  
  }  
  //---------------------------------------------------------------------------Top

6 楼tjzzx888(青草)回复于 2002-12-14 17:26:38 得分 0

to   ly_liyong(编程浪子)   (   )   :  
   
  xmlInterface是什么   呀,能详细讲一下吗Top

相关问题

  • 文件列表
  • 关于文件列表框的问题
  • swf文件的播放列表
  • 那里有文件列表框控件?
  • 怎么获得文件列表?
  • 如何获取FTP文件列表?
  • 如何取得文件列表框的文件名?
  • 关于显示文件夹和文件列表的问题
  • 如何获取一个文件夹下的文件名列表
  • 怎样用列表控件作出有多个列(包括名称、文件大小等列)的文件列表?

关键词

  • 文件
  • xml
  • xmlinterface
  • ihandle
  • searchrec
  • iattributes
  • sr
  • findnext
  • filelist
  • content

得分解答快速导航

  • 帖主:tjzzx888
  • invalid
  • hatumei
  • ly_liyong

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo