int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
FILE *fp;
if((fp=fopen("list.txt","r"))==NULL)
{exit(0);}
CString Contactors[20];
char s[20];
int Contac_Count=0;
do
{
fscanf(fp,"%s\n",s);
CString s1(s);
Contactors[Contac_Count++]=s1+"        ";
}while( !feof(fp) );
fclose(fp);
HTREEITEM hItem,hSubItem;
CTreeCtrl* pTree = (CTreeCtrl*)GetDlgItem(IDC_TREECTRL);
hItem = pTree->InsertItem("好友",TVI_ROOT);
hSubItem = pTree->InsertItem(Contactors[0],hItem);
hSubItem = pTree->InsertItem(Contactors[1],hItem,hSubItem);
hSubItem = pTree->InsertItem(Contactors[2],hItem,hSubItem); hItem = pTree->InsertItem("同事 ",TVI_ROOT,hItem);
hSubItem = pTree->InsertItem(Contactors[3],hItem);
hSubItem = pTree->InsertItem(Contactors[4],hItem,hSubItem);
hSubItem = pTree->InsertItem(Contactors[0],hItem,hSubItem); hItem = pTree->InsertItem("同学 ",TVI_ROOT,hItem);
hSubItem = pTree->InsertItem("Child3_1 ",hItem);
hSubItem = pTree->InsertItem("Child3_2 ",hItem,hSubItem);
hSubItem = pTree->InsertItem("Child3_3 ",hItem,hSubItem);
if (CUIWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CenterWindow();
// TODO: Add your message handler code here
lianxiren="查找联系人";
UpdateData(false);
return 0;
//create file to save content}list.txt里我保存的就是很多联系人的名字,现在能把这些名字读到主界面上。我想在这个create函数里接着实现,为每个树的子节点显示的名字都建立一个txt文档,而且文档名是这个形式:联系人.txt。我弄了半天实现不出来。哪位大虾帮个忙。

解决方案 »

  1.   

    那里实现有问题?你在InsertItem之后就创建文本不就行了直接fopen("test.txt","a");就行了
      

  2.   

    ?没明白你说的。我是想用个循环语句去为每个结点里显示的不同联系人,以联系人的名字.txt形式建立txt文件。不知道为什么我在给txt文档命名的时候获取不了树节点里显示的联系人。只能获取Contactors,得不到对应的值。
    急了
      

  3.   

    可以使用fopen 
    读: 
    FILE* fp=NULL; 
    CString FileName="c:\1.txt"; 
    fp = fopen(FileName.GetBuffer(0),"r"); 
    if (fp) 

    fscanf(fp,"%d\n",&m_Standard1num); 
            } 
    fclose(fp); 
    写: 
    FILE* fp=NULL; 
    CString FileName="c:\1.txt"; 
    fp = fopen(FileName.GetBuffer(0),"w"); 
    if (fp) 

    fprintf(fp,"%d\n",m_Standard1num); 

    fclose(fp); 
    字符用char[]型读取,保存可以用char[]或者cstring
      

  4.   

    楼上的没明白我的意思,你那样建立文件我会。我是要在create函数里自动遍历树节点中的联系人,然后给每个不同的联系人自动建立一个txt文档,文件名是以联系人.txt的形式。不知道我有没有说明白。
      

  5.   

    你的联系人的名字是中文的所以需要Unicode支持。