主题:[原创]【求助】关于文本读写问题!写入时,多了一个空行,读出时,多了一行乱码!!急急急!!
问题:写入文本时,多了一个空行,读取时,多显示了一行乱码!!!如何才能不保存空行,或读取文件时,过滤掉空行???
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define BUFFER 20
#define BUFFERINT 8
#define TEL 11
typedef struct{
char name[BUFFER];
int num;
string birthday;
char sex[BUFFER];
int telephone;
char email[BUFFER];
}TXL;
typedef struct{
TXL *elem;//[LIST_INIT_SIZE];
int length;
}SqList;
void Creat_Sq(SqList &L)
{
cout<<"请输入建立新表的人数:";
cin>>L.length;
L.elem =new TXL[L.length];
for(int i=0;i<L.length;i++)
{
cout<<"输入第"<<i+1<<"个人的信息:学号姓名 性别生日 电话号码电子邮件"<<endl;
cin>>L.elem[i].num>>L.elem[i].name>>L.elem[i].sex>>L.elem[i].birthday>>L.elem[i].telephone>>L.elem[i].email;
}
}
int LocateElem_Sq(SqList &L,int nb){
for(int i=0;;i++)
{
if(L.elem[i].num==nb)
{
cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
break;
}
if(i>=L.length)
{
cout<<"无此人"<<endl;break;
}
}
return OK;
}
int ListInsert_Sq(SqList &L,int i,TXL e){
SqList Q;
Q.elem =new TXL[L.length];
for(int k=0;k<L.length;k++)
Q.elem[k]=L.elem[k];
if(i<1||i>L.length+1) return ERROR;
else{
L.elem =new TXL[L.length+1];
for(int k=0;k<L.length;k++)
L.elem[k] =Q.elem[k];
}
for(int j=L.length;j>=i;j--)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.length++;
return OK;
}
int Deletelist_Sq(SqList &L,int i)
{
TXL p;
if((i<1)||(i>L.length)) return ERROR;
p=L.elem[i-1];
for(int j=i-1;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
return OK;
}
void Print_Sq(SqList &L)
{
cout<<"学号姓名性别生日电话号码电子邮件"<<endl;
for(int i=0;i<L.length;i++)
cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
}
void Save_Sq(SqList &L)
{
ofstream outfile("TXL.txt",ios::out);
if(!outfile)
{
cerr<<"TXL.txt can't open!"<<endl;
abort();
}
for(int i=0;i<L.length;i++)
{
outfile<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
}
outfile.close();
cout<<"保存成功"<<endl;
}
void Load_Sq(SqList &L)
{
ifstream infile;
L.elem=new TXL[100];
L.length=0;
infile.open("TXL.txt",ios::in);
if(!infile)
{
cerr<<"TXL.txt can't open!"<<endl;
abort();
}
while(!infile.eof())
{
infile>>L.elem[L.length].num>>L.elem[L.length].name>>L.elem[L.length].sex>>L.elem[L.length].birthday>>L.elem[L.length].telephone>>L.elem[L.length].email;
cout<<L.elem[L.length].num<<" "<<L.elem[L.length].name<<" "<<L.elem[L.length].sex<<" "<<L.elem[L.length].birthday<<" "<<L.elem[L.length].telephone<<" "<<L.elem[L.length].email<<endl;
L.length++;
}
infile.close();
}
void main()
{
int i,n,nb;
TXL e,p;
SqList L;
do {
cout<<"菜单选择如下:"<<endl;
cout<<"1 创建通讯录:"<<endl;
cout<<"2 查找操作:"<<endl;
cout<<"3 插入操作:"<<endl;
cout<<"4 删除操作:"<<endl;
cout<<"5 打印操作:"<<endl;
cout<<"6 保存文件:"<<endl;
cout<<"7 打开文件:"<<endl;
cout<<"8 退出程序:"<<endl;
cout<<"请输入相应操作的序号:";
cin>>n;
switch(n)
{
case 1:
{
Creat_Sq(L);
Print_Sq(L);
}break;
case 2:
{
cout<<"输入要查找联系人的学号: ";
cin>>nb;
LocateElem_Sq(L,nb);
}break;
case 3:
{
cout<<"输入插入的位置:";
cin>>i;
cout<<"请输入要插入的人的信息:";
cin>>e.num>>e.name>>e.sex>>e.birthday>>e.telephone>>e.email;
ListInsert_Sq(L,i,e);
Print_Sq(L);
}break;
case 4:
{
cout<<"输入删除的位置:";
cin>>i;
Deletelist_Sq(L,i);
Print_Sq(L);
}break;
case 5:
{
Print_Sq(L);
}
case 6:
{
Save_Sq(L);
}break;
case 7:
{
Load_Sq(L);
}break;
default:break;
}
}while(n>0&&n<8);
}
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define BUFFER 20
#define BUFFERINT 8
#define TEL 11
typedef struct{
char name[BUFFER];
int num;
string birthday;
char sex[BUFFER];
int telephone;
char email[BUFFER];
}TXL;
typedef struct{
TXL *elem;//[LIST_INIT_SIZE];
int length;
}SqList;
void Creat_Sq(SqList &L)
{
cout<<"请输入建立新表的人数:";
cin>>L.length;
L.elem =new TXL[L.length];
for(int i=0;i<L.length;i++)
{
cout<<"输入第"<<i+1<<"个人的信息:学号姓名 性别生日 电话号码电子邮件"<<endl;
cin>>L.elem[i].num>>L.elem[i].name>>L.elem[i].sex>>L.elem[i].birthday>>L.elem[i].telephone>>L.elem[i].email;
}
}
int LocateElem_Sq(SqList &L,int nb){
for(int i=0;;i++)
{
if(L.elem[i].num==nb)
{
cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
break;
}
if(i>=L.length)
{
cout<<"无此人"<<endl;break;
}
}
return OK;
}
int ListInsert_Sq(SqList &L,int i,TXL e){
SqList Q;
Q.elem =new TXL[L.length];
for(int k=0;k<L.length;k++)
Q.elem[k]=L.elem[k];
if(i<1||i>L.length+1) return ERROR;
else{
L.elem =new TXL[L.length+1];
for(int k=0;k<L.length;k++)
L.elem[k] =Q.elem[k];
}
for(int j=L.length;j>=i;j--)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.length++;
return OK;
}
int Deletelist_Sq(SqList &L,int i)
{
TXL p;
if((i<1)||(i>L.length)) return ERROR;
p=L.elem[i-1];
for(int j=i-1;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
return OK;
}
void Print_Sq(SqList &L)
{
cout<<"学号姓名性别生日电话号码电子邮件"<<endl;
for(int i=0;i<L.length;i++)
cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
}
void Save_Sq(SqList &L)
{
ofstream outfile("TXL.txt",ios::out);
if(!outfile)
{
cerr<<"TXL.txt can't open!"<<endl;
abort();
}
for(int i=0;i<L.length;i++)
{
outfile<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "<<L.elem[i].birthday<<" "<<L.elem[i].telephone<<" "<<L.elem[i].email<<endl;
}
outfile.close();
cout<<"保存成功"<<endl;
}
void Load_Sq(SqList &L)
{
ifstream infile;
L.elem=new TXL[100];
L.length=0;
infile.open("TXL.txt",ios::in);
if(!infile)
{
cerr<<"TXL.txt can't open!"<<endl;
abort();
}
while(!infile.eof())
{
infile>>L.elem[L.length].num>>L.elem[L.length].name>>L.elem[L.length].sex>>L.elem[L.length].birthday>>L.elem[L.length].telephone>>L.elem[L.length].email;
cout<<L.elem[L.length].num<<" "<<L.elem[L.length].name<<" "<<L.elem[L.length].sex<<" "<<L.elem[L.length].birthday<<" "<<L.elem[L.length].telephone<<" "<<L.elem[L.length].email<<endl;
L.length++;
}
infile.close();
}
void main()
{
int i,n,nb;
TXL e,p;
SqList L;
do {
cout<<"菜单选择如下:"<<endl;
cout<<"1 创建通讯录:"<<endl;
cout<<"2 查找操作:"<<endl;
cout<<"3 插入操作:"<<endl;
cout<<"4 删除操作:"<<endl;
cout<<"5 打印操作:"<<endl;
cout<<"6 保存文件:"<<endl;
cout<<"7 打开文件:"<<endl;
cout<<"8 退出程序:"<<endl;
cout<<"请输入相应操作的序号:";
cin>>n;
switch(n)
{
case 1:
{
Creat_Sq(L);
Print_Sq(L);
}break;
case 2:
{
cout<<"输入要查找联系人的学号: ";
cin>>nb;
LocateElem_Sq(L,nb);
}break;
case 3:
{
cout<<"输入插入的位置:";
cin>>i;
cout<<"请输入要插入的人的信息:";
cin>>e.num>>e.name>>e.sex>>e.birthday>>e.telephone>>e.email;
ListInsert_Sq(L,i,e);
Print_Sq(L);
}break;
case 4:
{
cout<<"输入删除的位置:";
cin>>i;
Deletelist_Sq(L,i);
Print_Sq(L);
}break;
case 5:
{
Print_Sq(L);
}
case 6:
{
Save_Sq(L);
}break;
case 7:
{
Load_Sq(L);
}break;
default:break;
}
}while(n>0&&n<8);
}