文件列表的问题
我想把目录树保存在一个.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




