ITPub博客

一个简单的文件管理程序 (转)

原创 ERP 作者:amyz 时间:2007-11-28 11:09:40 0 删除 编辑
一个简单的文件管理程序 (转)[@more@]

node.h

====================================

#include
template
class node
{
 private:
  node *next;   
 public:
  T data;
  node(void);
  node(T data1,node *p=NULL);
void insertafter(node *p);
void deleteafter(void);
node* nextnode(void);
};
template
node::node(void)
{   
  next=NULL;
}
template
node::node(T data1,node *p)
{
  data=data1;
  next=p;
}
template
void node::insertafter(node *p)
{
  p->next=next;
  next=p;
}
template
void node::deleteafter(void)
{
  node *temp;
  temp=next;
  next=temp->next;
  delete temp; 
}
template
node* node::nextnode(void)
{
 return next;
}

list.h

=====================================

#include
#include
#include"node.h"
template
class list
{
  private:
  node *head,*newnode,*last,*currnode,*prenode;
  public:
  list(void);
node*  setnode(T data2,node* p=NULL); 
void  createlist(T data2,node* p=NULL);
void  Insertlast(T p);
void  deletedata(T d);
void  print(void);
void  deletelist(void);
void  athead(void);
T  Data(void);
node*  Next(void);
node*  Currnode(void);
};
template
list::list(void)
{
  head=NULL;
  newnode=NULL;
  last=NULL;
  currnode=NULL;
  prenode=NULL;
}
template
node* list::setnode(T data2,node* p)
{
  newnode=new node(data2);
  return newnode;
}
template
void list::createlist(T data2,node* p=NULL)
{
  newnode=new node(data2);
  if(head==NULL)
  {
  head=newnode;
  }
  else
  {
  currnode=head;
  while(currnode->nextnode()!=NULL)
  {
  currnode=currnode->nextnode();
  }
  currnode->insertafter(newnode);
  currnode=newnode;
  }
  last=newnode; 
}
template
void list::Insertlast(T p)

 newnode=new node(p);
 if(last->nextnode()==NULL)
 {
  last->insertafter(newnode);
 }
 else
 {
  cout<<"there are some error in last"<  exit(0);
 } 
}
template
void list::deletedata(T d)
{
 if(head==NULL)
  {
  cout<<"no data in the list"<  exit(0);
  }   
 if(head->data==d)
 {
    head=head->nextnode();
 }
 else
 {
  currnode=head;
  while(currnode!=NULL)
  {
  if(currnode->data!=d)
 { 
 prenode=currnode; 
 currnode=currnode->nextnode();
  }
 else
  {
   prenode->deleteafter();
  currnode=prenode->nextnode();
 } 
  } 
 }
}
template
void list::print(void)
{
  currnode=head;
  if(head==NULL)
  {
  cout<<"no data in list"<  exit(0);
  }
  while(currnode!=NULL)
  {
  cout<data<<"  ";
  currnode=currnode->nextnode();
  } 
  cout<}
template
void list::deletelist(void)

 node *temp;
  if(head==NULL)
 {
  cout<<"no data in list"<  exit(0);
 }
  while(head!=NULL)
  {
  temp=head;
  head=head->nextnode();
 delete temp;
  }
  }
template
void list::athead(void)

 if(head!=NULL)
 {
  currnode=head;
 }
 else
 {
  cout<<"no data at head"< exit(0);
 }
}
template
T list::Data(void)
{
  if(currnode!=NULL)
  {
  return currnode->data;
  }
}
template
node* list::Next(void)

  if(currnode!=NULL)
  { 
  currnode=currnode->nextnode();
  return currnode;
  }
  else
  {
  return NULL;
  } 
 }
template
node* list::Currnode(void)
{
  return currnode;
}
guanli.cpp

============================================

#include
#include
#include
#include"list.h"
struct file  // file struct;
{
  string filename;
  string aboutfile;
  string filepath;
};
struct getfile
{
  string filename;
  string aboutfile;
  string filepath;
};
void main(void)
{
  int select
  file myfile;
  string space,findvalue;
  ofstream writefile;
  list mylist;
  char ch,text;
  int stringsize,i;
  for(;;)
  {
  string linevalue;  // jing zhi zai for(;;){} xuan huan zhong ding yi(1-3);
  string openname("/home/c++/zuoping2/jiru");// 2
  ifstream readfile(openname.c_str(),ios::in);//3
  string::size_type pos=0,pre=0;
  getfile getmyfile;
  cout<<"1:add file"<  cout<<"2:find file"<  cout<<"3:watch file"<  cout<<"4:delete file"<  cout<<"5:quite"<  cin>>select;
  switch(select)
  {
  case 1: 
  writefile.open("/home/c++/zuoping2/jiru",ios::out | ios::app); 
  if(!writefile)
  {
  cout<<"Don't open jiru file"<  exit(1);
  } 
  getline(cin,space,'n'); 
  cout<<"please input file name"<  getline(cin,myfile.filename,'n');
  cout<<"please input file description"<  getline(cin,myfile.aboutfile,'n');
  cout<<"please input file path"<  getline(cin,myfile.filepath,'n');
  if(myfile.filepath[myfile.filepath.size()-1]=='/')
  { 
 writefile<  }
  else
  {
  myfile.filepath=myfile.filepath+"/";
 writefile<  } 
  writefile.close(); 
  break;
  case 2:
  if(!readfile)
  {
  cout<<"Don't open jiru file"<  exit(-1); 
  }
  cout<<"please input file name for finding file"<  cin>>findvalue;
  while(getline(readfile,linevalue,'n'))
  { 
  pre=0;
  pos=0;
  //get file name;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.filename=linevalue.substr(pre,pos-pre);  ++pos;
  pre=pos;
  //get file description;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.aboutfile=linevalue.substr(pre,pos-pre);
  ++pos;
  pre=pos;
  //get file path;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.filepath=linevalue.substr(pre,pos-pre);
  ++pos;
  pre=pos;
  //get getmylist data for create list;
  mylist.createlist(getmyfile);
  } 
  mylist.athead();
  while(mylist.Currnode()!=NULL)
  {
  if(mylist.Data().filename==findvalue)  //when find file; 
  {
  cout<<"find file"<  cout<<"###########################################"<  cout<<"<"<  cout<<"###########################################"<  cout<<"do you want to like source"<  cin>>ch;
  if(ch=='y')
  {
 string filepath;
   filepath=mylist.Data().filepath+mylist.Data().filename;
  //erase char 'n';
 for(i=0;i<=filepath.size();i++)
 {
  if(filepath[i]=='n')
  {
  filepath.erase(i,1);
  }
 }
 //open file;
  ifstream openfiles;
 openfiles.open(filepath.c_str(),ios::in);
  if(!openfiles)
  {
  cerr<<"don't open file"<  exit(-1);
  }
  //read file context;
   while(!openfiles.eof())
 {
  openfiles.get(text);
  cout.put(text);
 }
  } 
  } 
   
  mylist.Next();
  }
  mylist.deletelist();   
  break;
  case 3:
  if(!readfile)
  {
  cerr<<"don't open the file"<  exit(-1);
  }
  while(getline(readfile,linevalue,'n'))
  {
  //get file name;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.filename=linevalue.substr(pre,pos-pre);  ++pos;
  pre=pos;
  //get file description;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.aboutfile=linevalue.substr(pre,pos-pre);
  ++pos;
  pre=pos;
  //get file path;
  pos=linevalue.find_first_of(';',pos);
  getmyfile.filepath=linevalue.substr(pre,pos-pre);
  ++pos;
  pre=pos;
  //get getmylist data for create list;
  mylist.createlist(getmyfile);
  pos=0;
  pre=0;
  }
  mylist.athead();
  while(mylist.Currnode()!=NULL)
  { 
  string filepath,descp;
  cout<<"the file description"<  cout<  cout<<"*****************************************************************"<  cout<<"do you want to look source"<  cin>>ch;
  if(ch=='y')
  {
   filepath=mylist.Data().filepath+mylist.Data().filename;
  pos=0;
  for(i=0;i<=filepath.size();i++)
 {
  if(filepath[i]=='n')
  {
  filepath.erase(i,1);
  }
 }
  cout<  cout<  ifstream openfiles;
 openfiles.open(filepath.c_str(),ios::in);
  if(!openfiles)
  {
  cerr<<"don't open file"<  exit(-1);
  }
   while(!openfiles.eof())
 {
  openfiles.get(text);
  cout.put(text);
 }
  }
  else if(ch=='q')
  {
  break;
  }
  mylist.Next();
  }
  mylist.deletelist(); 
  break; 
  case 5:
  exit(0); 
  }
 
  }

makefile 
========================================================

guanli:guanli.o list.h node.h
 g++ -g -I/home/c++/zuoping2/ guanli.cpp -o guanli
all:guanli
.PHONY:clean
clean:
 RM guanli.o
 

 


 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-985508/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3994
  • 访问量
    7257740