类版本_新闻管理系统
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct st_list
{
char address[50];
char author[20];
char title[50];
char text[2050];
};
template<class T> class news
{
public:
news(T *);
void add();
void update();
void display();
void find();
protected:
char address[50];
st_list news_list;
fstream text_file;
};
template<class T> news<T>::news(T addrs[])
{
if(sizeof(addrs)==0)
{
cout <<"地址不能为空!"<<endl;
return;
}
strcpy(address,addrs);
}
template<class T> void news<T>::add()
{
cout << "请输入文章标题:" << endl;
cin >> news_list.title;
cout << "请输入正文内容:" << endl;
cin >> news_list.text;
cout << "请输入作者信息:" << endl;
cin >> news_list.author;
text_file.open(address,ios::in|ios::out|ios::ate|ios::app);
text_file <<news_list.title<<'\n'<<news_list.text<<'\n'<<news_list.author<<'\n';
cout <<"\n"<<endl;
cout <<"成功插入!"<<endl;
cout <<"\n"<<endl;
text_file.close();
}
template<class T>
void news<T>::update()
{
char index[3000],swc[3000];
int position;
cout<<"输入标题:"<<endl;
cin>>swc;
text_file.open(address,ios::in|ios::out);
while(!text_file.eof())
{
text_file.getline(index,3000,'\n');
if(strcmp(index,swc)==0)
{
position=sizeof(swc);
text_file.seekg(position,ios::beg);
cout << "找到位置!" <<endl;
cout << "请输入文章标题:" << endl;
cin >> news_list.title;
cout << "请输入正文内容:" << endl;
cin >> news_list.text;
cout << "请输入作者信息:" << endl;
cin >> news_list.author;
text_file.open(address,ios::in|ios::out);
text_file <<'\n'<<news_list.title<<'\n'<<news_list.text<<'\n'<<news_list.author<<'\n';
cout <<"\n"<<endl;
cout <<"更新成功!"<<endl;
cout <<"\n"<<endl;
text_file.close();
return;
}
}
cout << "没有找到数据!" <<endl;
}
template<class T>
void news<T>::display()
{
char s;
text_file.open(address,ios::in|ios::out);
while((s=text_file.get())!=EOF)
{
cout <<s;
}
text_file.close();
cout <<"\n"<<endl;
}
template<class T>
void news<T>::find()
{
char index[3000],swc[3000],text[2050];
cout<<"输入标题:"<<endl;
cin>>swc;
text_file.open(address,ios::in|ios::out);
while(!text_file.eof())
{
text_file.getline(index,3000,'\n');
if(strcmp(index,swc)==0)
{
cout << "查找成功!" <<endl;
cout << "正文:" <<endl;
text_file.getline(text,2050,'\n');
cout <<text <<endl;
cout << "作者:" <<endl;
text_file.getline(text,2050,'\n');
cout <<text <<endl;
return;
}
}
cout << "没有找到数据!" <<endl;
}
void main()
{
char address[30];
int x;
cout <<"请输入文件地址!"<<endl;
cin >>address;
news<char> newss(address);
while(1)
{
cout<<"1.加入文章"<<endl;
cout<<"2.查找文章"<<endl;
cout<<"3.更新文章"<<endl;
cout<<"4.显示全部文章"<<endl;
cout<<"0.退出系统"<<endl;
cin>>x;
switch(x)
{
case 1:newss.add();break;
case 2:newss.find();break;
case 3:newss.update();break;
case 4:newss.display();break;
case 0:return;;break;
}
}
}
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17635016.html