分享
 
 
 

一个简单的文件管理程序

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

node.h

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

#include<iostream>

template<class T>

class node

{

private:

node<T> *next;

public:

T data;

node(void);

node(T data1,node<T> *p=NULL);

void insertafter(node<T> *p);

void deleteafter(void);

node<T>* nextnode(void);

};

template<class T>

node<T>::node(void)

{

next=NULL;

}

template<class T>

node<T>::node(T data1,node<T> *p)

{

data=data1;

next=p;

}

template<class T>

void node<T>::insertafter(node<T> *p)

{

p->next=next;

next=p;

}

template<class T>

void node<T>::deleteafter(void)

{

node<T> *temp;

temp=next;

next=temp->next;

delete temp;

}

template<class T>

node<T>* node<T>::nextnode(void)

{

return next;

}

list.h

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

#include<iostream>

#include<stdlib.h>

#include"node.h"

template<class T>

class list

{

private:

node<T> *head,*newnode,*last,*currnode,*prenode;

public:

list(void);

node<T>* setnode(T data2,node<T>* p=NULL);

void createlist(T data2,node<T>* p=NULL);

void Insertlast(T p);

void deletedata(T d);

void print(void);

void deletelist(void);

void athead(void);

T Data(void);

node<T>* Next(void);

node<T>* Currnode(void);

};

template<class T>

list<T>::list(void)

{

head=NULL;

newnode=NULL;

last=NULL;

currnode=NULL;

prenode=NULL;

}

template<class T>

node<T>* list<T>::setnode(T data2,node<T>* p)

{

newnode=new node<T>(data2);

return newnode;

}

template<class T>

void list<T>::createlist(T data2,node<T>* p=NULL)

{

newnode=new node<T>(data2);

if(head==NULL)

{

head=newnode;

}

else

{

currnode=head;

while(currnode->nextnode()!=NULL)

{

currnode=currnode->nextnode();

}

currnode->insertafter(newnode);

currnode=newnode;

}

last=newnode;

}

template<class T>

void list<T>::Insertlast(T p)

{

newnode=new node<T>(p);

if(last->nextnode()==NULL)

{

last->insertafter(newnode);

}

else

{

cout<<"there are some error in last"<<endl;

exit(0);

}

}

template<class T>

void list<T>::deletedata(T d)

{

if(head==NULL)

{

cout<<"no data in the list"<<endl;

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<class T>

void list<T>::print(void)

{

currnode=head;

if(head==NULL)

{

cout<<"no data in list"<<endl;

exit(0);

}

while(currnode!=NULL)

{

cout<<currnode->data<<" ";

currnode=currnode->nextnode();

}

cout<<endl;

}

template<class T>

void list<T>::deletelist(void)

{

node<T> *temp;

if(head==NULL)

{

cout<<"no data in list"<<endl;

exit(0);

}

while(head!=NULL)

{

temp=head;

head=head->nextnode();

delete temp;

}

}

template<class T>

void list<T>::athead(void)

{

if(head!=NULL)

{

currnode=head;

}

else

{

cout<<"no data at head"<<endl;

exit(0);

}

}

template<class T>

T list<T>::Data(void)

{

if(currnode!=NULL)

{

return currnode->data;

}

}

template<class T>

node<T>* list<T>::Next(void)

{

if(currnode!=NULL)

{

currnode=currnode->nextnode();

return currnode;

}

else

{

return NULL;

}

}

template<class T>

node<T>* list<T>::Currnode(void)

{

return currnode;

}

guanli.cpp

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

#include<iostream>

#include<string>

#include<fstream>

#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<getfile> 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"<<endl;

cout<<"2:find file"<<endl;

cout<<"3:watch file"<<endl;

cout<<"4:delete file"<<endl;

cout<<"5:quite"<<endl;

cin>>select;

switch(select)

{

case 1:

writefile.open("/home/c++/zuoping2/jiru",ios::out | ios::app);

if(!writefile)

{

cout<<"Don't open jiru file"<<endl;

exit(1);

}

getline(cin,space,'\n');

cout<<"please input file name"<<endl;

getline(cin,myfile.filename,'\n');

cout<<"please input file description"<<endl;

getline(cin,myfile.aboutfile,'\n');

cout<<"please input file path"<<endl;

getline(cin,myfile.filepath,'\n');

if(myfile.filepath[myfile.filepath.size()-1]=='/')

{

writefile<<myfile.filename<<";"<<myfile.aboutfile<<";"<<myfile.filepath<<";"<<'\n';

}

else

{

myfile.filepath=myfile.filepath+"/";

writefile<<myfile.filename<<";"<<myfile.aboutfile<<";"<<myfile.filepath<<";"<<'\n';

}

writefile.close();

break;

case 2:

if(!readfile)

{

cout<<"Don't open jiru file"<<endl;

exit(-1);

}

cout<<"please input file name for finding file"<<endl;

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"<<endl;

cout<<"###########################################"<<endl;

cout<<"<"<<mylist.Data().aboutfile<<"<"<<endl;

cout<<"###########################################"<<endl;

cout<<"do you want to like source"<<endl;

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"<<endl;

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"<<endl;

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"<<endl;

cout<<mylist.Data().aboutfile<<endl;

cout<<"*****************************************************************"<<endl;

cout<<"do you want to look source"<<endl;

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<<filepath;

cout<<endl;

ifstream openfiles;

openfiles.open(filepath.c_str(),ios::in);

if(!openfiles)

{

cerr<<"don't open file"<<endl;

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有