如何在所读取的文件中实现“添加,删除,保存,销毁”的功能??
能否用二叉树来完成??
比如,我已将boot.ini读取在文件a.txt中,并要实现简单操作,
如:添加,删除,保存,销毁等功能,是不是要用二叉树来解决啊?
如何在以下程序中实现:insert();delete();save();destroy()的功能?以下是读取boot.ini的程序:#include <stdio.h>
#include <conio.h>
#include <string.h> 
void kongge(char *x,FILE *y);
void main() 
{
FILE *fp1,*fp2;
char line1[1000],line2[1000],line3[1000];
char *m,*n,*pdest1,*pdest2,*pdest3,*pdest4;
    char *i,*a;
fp1=fopen("c:boot.ini","r");
fp2=fopen("a.txt","w");
while(!feof(fp1))
{
if(fgets(line1,1000,fp1)!=NULL)
{
if((a=strchr(line1,';'))!=NULL) *a='\0';
if(strchr(line1,';')==NULL)

if((strchr(line1,'[')!=NULL)&&(strchr(line1,'=')==NULL))  /*有[号,没有=号*/
{
    strcpy(line3,line1);
   if(strchr(line3,']')!=NULL)
   {   
   kongge(line3,fp2);
   }
}
else if(strchr(line1,'=')!=NULL)/*有等号*/
{
if(strchr(line1,' ')!=NULL)/*有空格*/
{
strcpy(line2,line1);
i=strchr(line2,'=');
pdest1=line2;m=line2;
pdest2=i-1;
pdest3=i+1;
n=i+1;
pdest4=line2+strlen(line2)-2;
while(*pdest1==' ')pdest1++;
while(*pdest2==' ')pdest2--;
while(*pdest3==' ')pdest3++;
while(*pdest4==' ')pdest4--;
if(pdest1>=m)
{
while(pdest1<=pdest2)

{*m=*pdest1;
 m++;pdest1++;
};
*m='=';m++;
if(m<=n)
{
if(pdest3>=n)
{while(pdest3<=pdest4)
{*m=*pdest3;
*m++;pdest3++;
};
*m='\0';
fputs(line2,fp2);
fputc('\n',fp2);
puts(line2);
}
}
}
}

else 
{
fputs(line1,fp2);
puts(line1);
}  
}
}}
}
fclose(fp1);
fclose(fp2);
getch();
}
void kongge(char *x,FILE *y)
{
char *n,*w,*begin,*end,*pdest5;
pdest5=strchr(x,']');
*(pdest5+1)='\0';
n=strchr(x,'[');
w=n+1;
begin=n+1;
end=n+strlen(x)-2;
while(*begin==' ') begin++;
while(*end==' ') end--;
if(begin>=w)
{
while(begin<=end)
{
*w=*begin;
w++;begin++;
};
*w=']';
w++;
} ;
*(w)='\0';
fputs(x,y);
fputs("\n",y);
puts(x);
}

 

解决方案 »

  1.   

    建议用结构处理,这样的话整个程序结构将会很清楚而且易操作..
    1.将你要操作的文件数据抽象成一个结构体或类.
    typedef struct {
       //变量定义
    } FILE_DATA_ITEM;
    class CFileData
    {
        CObList m_cObList;
    public:
        void Add(FILE_DATA_ITEM *pItem);
        void DeleteBy(TYPE unique);
        void Save();
        void Destroy();
    }
    2.逐行读入文件数据, 解析后调用CFileData::Add
    3.剩下的就是对内存的操作和一些简单的文件操作了
      

  2.   

    as if you`ve read out all from file..
    now I just give you a simple example:(I regard you`ve analysed the boot.ini and read it in a your.txt file just the follow format:
    your.txt
    -------------------------------------
    Key1=1
    Key2=2;
    Key3=3;
    ...
    then define
    extern CFileData fileData;
    typedef struct {
       CString key;
       int value;
    } FILE_DATA_ITEM;
    and read all data from your.txt line by line
    CFile cFileTxt;
    ...// file operation here
    while(TRUE)
    {
        CString strLine;
        if (!cFile.Read(strLine...))  // read a line
           break;                     // if feof break;
        CString key = .... // read the value from the your.txt here
        int value = .. // read the value from the your.txt here
        FILE_DATA_ITEM item;
        item.key = key;
        item.value = value;
        fileData.Add(&item);
    }
    and I give a implement of Add(FILE_DATA_ITEM *pItem) as the follow:
    void CFileData::Add(FILE_DATA_ITEM *pItem)
    {
       m_cObList.Add((CObList *)pItem;
    }
      

  3.   

    and the other, you can do it by yourself,
    it has no relation with class; in a detail function, the same as C program..
      

  4.   

    非常感谢你!mty(天鱼) 
    我会给你分的!
      

  5.   

    mty(天鱼) 你好,
    我非常想向你进一步学习,
    你有oicq吗??能不能告诉我啊?
    我是qq是30352157
    希望你能加我,迫切盼望你的答复!