...
借鉴了前面好几位博主造出来的轮子做出来这个。
存储方式:通过存储在文本当中实现具有的功能:增加,删除,修改,查看。
直接放代码吧
#include <fstream>
#include <iostream>
#include <cstdlib>
#include<regex>
#include<string>
#include<cstdlib>
#include<deque>
#include<iomanip>
using namespace std;
show(){//展示内容
char buffer[256]; //定义一个数组,用来存放字符
ifstream examplefile1("tongxunlu.txt");//读取
int i=-1; //声明一个对象与要读的文件联系
if (! examplefile1.is_open()) //
{
cout << "Error opening file";
exit (1);
}
while (!examplefile1.eof())
{
i++;
examplefile1.getline(buffer,100);
cout<<i<<buffer<< endl;
}
return 0;
}
add(){
string a,b,c,d;
ofstream examplefile;//写入
examplefile.open("tongxunlu.txt",ios::out|ios::app);
cout<<"输入姓名"<<endl;
cin>>a;
cout<<"输入电话号码"<<endl;
cin>>b;
cout<<"输入电子邮件"<<endl;
cin>>c;
cout<<"输入公司"<<endl;
cin>>d;
if (examplefile.is_open())
{
examplefile<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<setw(10)<<d<<endl;
examplefile.close();
}
return 0;
}
dele( )
{
ifstream file("tongxunlu.txt");
string line;
int n, count = 0;
ofstream outfile("test2.txt", ios::out | ios::trunc);
cout << "输出你想删除的行数:" << endl;
cin >> n;
while (!file.eof()) {
getline(file, line);
if (count != n )//如果要修改内容就在这修改line的内容,再存到文件中就行了
outfile << line << endl;
count++;
}
outfile.close();
file.close();
ofstream outfile1("tongxunlu.txt", ios::out | ios::trunc);
fstream file1("test2.txt");
while (!file1.eof()) {
getline(file1, line);
outfile1 << line << endl;
}
outfile1.close();
file1.close();
system("del test2.txt");//删除中间文件
}
insr(){
string a,b;
ifstream file("tongxunlu.txt");
string line;
int n, count = 0;
ofstream outfile("test2.txt", ios::out | ios::trunc);
cout << "输出你修改的行数:" << endl;
cin >> n;
cout << "请输入姓名:" << endl;
cin>>a;
cout<<"请输入电话号码"<<endl;
cin>>b;
while (!file.eof()) {
getline(file, line);
if (count != n )//如果要修改内容就在这修改line的内容,再存到文件中就行了
outfile << line << endl;
else {
outfile<<setw(10)<<a<<setw(10)<<b<<endl;
}
count++;
}
outfile.close();
file.close();
ofstream outfile1("tongxunlu.txt", ios::out | ios::trunc);
fstream file1("test2.txt");
while (!file1.eof()) {
getline(file1, line);
outfile1 << line << endl;
}
outfile1.close();
file1.close();
system("del test2.txt");//删除中间文件
}
delspace(){
FILE *fp = fopen("tongxunlu.txt","r");
FILE *tmp = tmpfile();
char ch;
int t = 0;
while((ch=fgetc(fp)) != EOF) {
if(ch == '\n' && !t) //如果每行的第一个字符是换行符,就跳过这一行
continue;
if(ch == '\n' && t) { //如果一行的第一个字符不是换行符就将这一行的内容输入到临时文件
t = 0;
fputc(ch,tmp);
}
else {
fputc(ch,tmp);
t = 1;
}
}
rewind(tmp); //重定位临时文件的文件指针到开头
fclose(fp);
fopen("tongxunlu.txt","w"); //重写以写方式打开源文件
while((ch=fgetc(tmp)) != EOF) { //将临时文件里的内容复制到源文件
fputc(ch,fp);
}
fclose(tmp); //分别关闭两个文件
fclose(fp);
}
int main()
{
int i=0;
int order;
ofstream examplefile;//写入列表消息
examplefile.open("tongxunlu.txt",ios::out|ios::app);
examplefile <<setw(10)<<"姓名"<<setw(10)<<"电话"<<setw(10)<<"电子邮件"<<setw(10)<<"公司"<<endl;
for(;;){
cout<<"请输入对应字符进行对应的操作"<<endl;
cout<<"0:退出,1:添加,2:查看,3:删除,4:修改,5:清屏"<<endl;
cin>>order;
if (order==0){
break;
}else{
switch (order){
case 1:{
add();
break;//添加功能
}
case 2:{ delspace();
show();
break;//展示功能
}
case 3:{
dele();
break;//删除功能
}
case 4:{
insr();
break;//删除功能
}
case 5:{
system("cls");
break;//删除功能
}
}
}
}
return 0;
}