我不明白为什么会发生这样,求解!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include//bits/stdc++.h
#include//help.h
cin>>name;
string c;
ifstream filein14("情况.txt",ios::in);
while(getline(filein14,c))
{
length_word[flag]=c.length()-1;
debug(length_word[flag]);
flag+=1;
if(c==name)
{
flag2=1;
break;
}
}
help.h的内容:
#include//bits/stdc++.h
using namespace std;
void debug(int a)
{
cout<<a<<endl;
}
void debug(char a)
{
cout<<a<<endl;
}
void debug(char* a)
{
cout<<a<<endl;
}
void debug(string a)
{
cout<<a<<endl;
}
情况.txt的内容(有部分中文乱码):
awdrgy
#1.0#3-3
dorothy
#2.2#0-0
sssusie
#1.0#3-0
ߴߴ
#1.0#4-0
perry
#4.0#7-2
Ʈ������
#4.0#7-0
输出:
8
7
6
7
6
7
3
7
4
解释:我输出了文件每行的长度
谁能告诉我为什么?
c在哪里定义的
c == name
如果c是char[]的话,应该用 strcmp 而不是 == 比较。
还有你的问题是什么,乱码么?中文的编码是双字节的,你按照char去读写要小心。
全部代码(从后面看):
#include//bits/stdc++.h
#include"help.h"
using namespace std;
int main()
{
for( ; ; )
{
string control="",ifend="",c="",swap="",name="",passport="",h="",n="",inspect_type="",test_name="",inspect_name[30],inspect_password[30];
char l,namebychar[20];
int No=0,No2=0,flag=0,num[30][100],t=0,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0,flag8=0,m=0,o=0,r=0,length_name[30],length_password[30][100],k[30][100][6],num_name,sum[3],money,buy[5],length_word[30];
for(int t=0;t<30;t++)
{
for(m=0;m<100;m++)
{
memset(k[t][m],0,sizeof(k[t][m]));
}
memset(length_word,0,sizeof(length_word));
}
memset(length_password,0,sizeof(100));
cout<<"请问要进行什么操作?"<<endl<<"(注册/查看/更新/购买/还债)"<<endl;
cin>>control;
if(control=="注册")
{
fstream testbycharfile;
testbycharfile.open("名单2.txt",ios::in);
while(!testbycharfile.eof())
{
testbycharfile>>l;
if(l=='$')
{
No+=1;
}
}
for( ; ; )
{
cout<<"请输入昵称"<<endl;
cin>>name;
ifstream filein8("名单.txt",ios::in);
while(filein8>>c)
{
if(c==name)
{
flag6=1;
break;
}
}
if(flag6==1)
{
cout<<"昵称已存在!";
flag6=0;
continue;
}
cout<<endl<<"请输入密码"<<endl;
cin>>passport;
for(int t_1=0;t_1<10;t_1++)
{
for(int t_2=0;t_2<10;t_2++)
{
for(int t_3=0;t_3<t_1;t_3++)
{
test_name+=t_2+'0';
if(passport==(name+test_name))
{
cout<<"密码过于简单!";
flag6=1;
break;
}
if(passport==test_name)
{
cout<<"密码过于简单!";
flag6=1;
break;
}
}
if(flag6==1)
{
break;
}
test_name="";
}
if(flag6==1)
{
break;
}
}
if(flag6==1)
{
flag6=0;
continue;
}
cout<<endl;
cout<<"请确认信息" <<endl<<"昵称:"<<name<<endl<<"编号:"<<No<<endl<<"密码:"<<passport<<"(输入是或否)"<<endl;
cin>>h;
if(h=="是")
{
break;
}
else if(h=="否")
{
}
else
{
cout<<"已默认为“否”";
}
}
testbycharfile.open("名单.txt",ios::in);
ofstream fileOut("名单2.txt",ios::app);
while(!testbycharfile.eof())
{
testbycharfile>>c;
fileOut<<c;
}
fileOut<<No<<"#"<<name<<"#"<<passport<<"$";
ofstream fileio2("名单.txt",ios::app);
fileio2<<endl<<No<<endl<<name<<endl<<passport<<endl<<"&";
srand((unsigned)time(NULL));
for(int i=0;i<100;i++)
{
num[0][i]=rand()%999999;
}
ofstream file2("密码.txt",ios::app);
file2<<endl<<name;
for(int t=0;t<100;t++)
{
file2<<endl<<num[0][t];
}
file2<<endl<<"&";
for( ; ; )
{
cout<<"请输入钱数"<<endl;
cin>>money;
if(money>7)
{
cout<<"过多!(集数请另外购买)";
continue;
}
cout<<"请确认(是/否)"<<endl;
cin>>h;
if(h=="否")
{
continue;
}
else if(h!="是")
{
cout<<"已默认为\"否\"";
}
ofstream fileout13("情况.txt",ios::app);
fileout13<<name;
for(t=3;t<9;t++)
{
if(money<t)
{
fileout13<<" "<<t-4<<"。0 "<<money<<"-0";
break;
}
}
fileout13<<endl;
break;
}
}
else if(control=="查看")
{
cout<<"请输入查看类型(个人/查看码)"<<endl;
cin>>inspect_type;
if(inspect_type=="个人")
{
cout<<"请输入你要查看的名字"<<endl;
cin>>name;
ifstream filein("名单.txt",ios::in);
flag=0;
while(filein>>c)
{
if(c=="&")
{
++flag;
}
if(c==name)
{
ifstream filein3("名单.txt",ios::in);
while(filein3>>c)
{
if(flag==0)
{
cout<<"编号:"<<c<<endl;
flag3=1;
flag++;
}
if(flag2==1)
{
cout<<"昵称:"<<c<<endl;
}
if(flag2==2)
{
cout<<"密码:"<<c<<endl;
}
if(flag3==1)
{
flag2++;
if(c=="&")
{
break;
}
}
if(c=="&")
{
flag-=1;
}
}
flag=1;
break;
}
}
if(flag==1)
{
ifstream filein2("密码.txt",ios::in);
cout<<"查看码:";
flag4=0;
flag3=0;
while(filein2>>c)
{
if(c=="&")
{
flag3++;
}
if(c==name)
{
ifstream filein4("密码.txt",ios::in);
while(filein4>>c)
{
if(flag3==0&&flag4==0)
{
flag4++;
continue;
}
if(c=="&")
{
flag3--;
}
if(flag4==1)
{
if(c=="&")
{
break;
}
cout<<endl<<c;
}
}
cout<<endl;
}
}
}
else
{
cout<<"用户不存在!";
}
}
else if(inspect_type=="查看码")
{
cout<<"请输入集数(n~m)"<<endl;
cin>>No>>l>>No2;
ifstream filein5("密码.txt",ios::in);
flag=0;
flag5=0;
while(filein5>>c)
{
if(No>100)
{
cout<<"集数不存在!";
break;
}
if(c=="&")
{
flag=-2;
}
flag+=1;
if(flag==0)
{
cout<<c<<endl;
}
if(flag==No)
{
if(flag5==0)
{
flag5+=1;
flag-=1;
}
while(flag<=No2)
{
filein5>>c;
cout<<" "<<c<<endl;
flag++;
}
}
}
}
}
else if(control=="更新")
{
ifstream filein6("密码.txt",ios::in);
flag5=-1;
flag4=0;
flag3=0;
flag=0;
flag2=0;
while(filein6>>c)
{
if(c=="&")
{
flag5++;
flag=1;
flag2++;
continue;
}
if(flag==2)
{
length_password[flag5][flag3]=c.size();
flag3++;
}
if(flag==1)
{
length_name[flag4]=c.size();
inspect_name[flag4]=c;
flag4++;
flag=2;
}
}
num_name=flag4;
flag=0;
m=0;
o=0;
flag3=0;
flag4=-1;
flag5=-1;
fstream testbycharfile;
testbycharfile.open("密码.txt",ios::in);
while(testbycharfile>>l)
{
if(l=='&')
{
flag=0;
}
if(flag==1)
{
if(length_name[flag4]>0)
{
length_name[flag4]-=1;
continue;
}
o=length_password[flag5][flag3];
if(m==0)
{
--o;
}
k[flag5][m][o]=l-'0';
if(o==0)
{
flag3+=1;
m++;
}
length_password[flag5][flag3]-=1;
}
if(l=='&')
{
flag5++;
flag2-=1;
flag4++;
flag=1;
m=0;
}
}
ifstream filein10("名单.txt",ios::in);
while(filein10>>c)
{
if(flag7==3)
{
inspect_password[r]=c;
flag7=0;
r++;
}
if(flag7==2)
{
flag7++;
}
if(flag7==1)
{
flag7++;
}
if(c=="&")
{
flag7=1;
}
}
for(int t=0;t<100;t++)
{
num[0][t]=k[0][t][5]*100000+k[0][t][4]*10000+k[0][t][3]*1000+k[0][t][2]*100+k[0][t][1]*10+k[0][t][0];
}
ofstream fileout7("我的我的世界Bug日记4.2.4.cpp",ios::out);
fileout7<<"#include<iostream>\n#include<fstream>\n#include<cstring>\nusing namespace std;\nint main()\n{\n int b,flag;\n string name,password;\n char a;\n cout<<\"请输入昵称\";\n cin>>name;\n cout<<\"请输入密码\";\n cin>>password;\n if(name==\""<<inspect_name[0]<<"\")\n {\n if(password==\""<<inspect_password[0]<<"\")\n {\n cout<<\"请问你要看哪一期?\";\n cin>>a;\n cout<<\"请输入查看码\";\n cin>>b;\n switch(a)\n {\n";
for(int t=0;t<100;t++)
{
fileout7<<" case'"<<t+1<<"':\n if(b=="<<num[0][t]<<")\n {\n flag=1;\n }\n else\n {\n flag=2;\n }\n break;\n";
}
fileout7<<" }\n if(flag==1)\n {\n switch(a)\n {\n case'1':\n {\n ofstream fileOut(\"我的世界Bug日记第1期.txt\",ios::out);\n fileOut<<\"Bug出生在一个荒岛上,\"<<endl<<\"他不是新手,所以知道要砍树。\"<<endl<<\"他看了看四周,发现没有树,于是他往前走\"<<endl<<\"走着走着,他瞬移了,卡到一棵树里\"<<endl<<\"卡死了。\"<<endl<<\"第一期结束\";\n break;\n }\n case'2':\n {\n ofstream fileOut2(\"我的世界Bug日记第二期.txt\",ios::out);\n fileOut2<<\"Bug重生了,但他却不在那个岛上\"<<endl<<\"他在山洞里,发现一个宝箱,里面有本书\"<<endl<<\"他打开一看,里面写着:重生点更改 100%\"<<endl<<\"瞬移 25%\"<<endl<<\"后面都是空的,他想:这难道是我卡Bug的几率?\"<<endl<<\"他突然被小白围攻,他搭起了一堵墙,结果还是死了。\"<<endl<<\"书上默默地多了一条:\"<<endl<<\"弓箭穿墙 30%\"<<endl<<\"第二期结束\";\n }\n default:cout<<\"尚未开发\";\n }\n }\n }\n }\n else if(flag==2)\n {\n cout<<\"密码错误!\";\n }\n";
ofstream fileout8("我的我的世界Bug日记4.2.4.cpp",ios::app);
for(int i=1;i<num_name;i++)
{
for(int t=0;t<100;t++)
{
num[i][t]=k[i][t][5]*100000+k[i][t][4]*10000+k[i][t][3]*1000+k[i][t][2]*100+k[i][t][1]*10+k[i][t][0];
}
fileout8<<"else if(name==\""<<inspect_name[i]<<"\")\n {\n if(password==\""<<inspect_password[i]<<"\")\n {\n cout<<\"请问要看哪一期?\";\n cin>>a;\n cout<<\"请输入查看码\";\n cin>>b;\n switch(a)\n {\n";
for(int t=0;t<100;t++)
{
fileout8<<" case'"<<t+1<<"':\n if(b=="<<num[i][t]<<")\n {\n flag=1;\n }\n else\n {\n flag=2;\n }\n break;\n";
}
fileout8<<" }\n if(flag==1)\n {\n switch(a)\n {\n case'1':\n {\n ofstream fileOut(\"我的世界Bug日记第1期.txt\",ios::out);\n fileOut<<\"Bug出生在一个荒岛上,\"<<endl<<\"他不是新手,所以知道要砍树。\"<<endl<<\"他看了看四周,发现没有树,于是他往前走\"<<endl<<\"走着走着,他瞬移了,卡到一棵树里\"<<endl<<\"卡死了。\"<<endl<<\"第一期结束\";\n break;\n }\n case'2':\n {\n fstream fileOut2(\"我的世界Bug日记第二期.txt\",ios::out);\n fileOut2<<\"Bug重生了,但他却不在那个岛上\"<<endl<<\"他在山洞里,发现一个宝箱,里面有本书\"<<endl<<\"他打开一看,里面写着:重生点更改 100%\"<<endl<<\"瞬移 25%\"<<endl<<\"后面都是空的,他想:这难道是我卡Bug的几率?\"<<endl<<\"他突然被小白围攻,他搭起了一堵墙,结果还是死了。\"<<endl<<\"书上默默地多了一条:\"<<endl<<\"弓箭穿墙 30%\"<<endl<<\"第二期结束\";\n }\n default:cout<<\"尚未开发\";\n }\n }\n }\n }\n else if(flag==2)\n {\n cout<<\"密码错误!\";\n }\n";
}
fileout8<<" return 0;\n}";
}
else if(control=="购买")
{
cout<<"请输入昵称"<<endl;
cin>>name;
cout<<"请输入集数(n~m)"<<endl;
cin>>num[0][0]>>l>>num[0][1];
if(l!='~')
{
flag==5;
}
if(num[0][0]>num[0][1])
{
num[0][2]=num[0][0];
num[0][0]=num[0][1];
num[0][1]=num[0][2];
}
ifstream filein11("密码.txt",ios::in);
while(filein11>>n)
{
if(n==name)
{
flag=1;
for(int t=1;t<num[0][0];t++)
{
filein11>>n;
if(n=="&")
{
flag5=1;
break;
}
}
if(flag5==1)
{
break;
}
cout<<"查看码为:"<<endl;
for(int t=1;t<=num[0][1]-num[0][0]+1;t++)
{
filein11>>n;
cout<<" "<<n<<endl;
}
break;
}
flag=0;
}
sum[0]=num[0][1]-num[0][0]+1;
sum[1]=sum[0]/5;
if(flag5==1)
{
cout<<"集数不存在!";
}
else if(sum[1]*5!=sum[0])
{
sum[2]=5-(sum[0]-sum[1]*5);
sum[1]++;
}
else
{
sum[2]=0;
}
if(flag5!=1)
{
cout<<"总价="<<sum[1]<<"-"<<sum[2]<<endl<<"请输入已付"<<endl;
cin>>money;
if(sum[1]<money)
{
buy[0]=money-sum[1];
}
else if(sum[1]>money)
{
buy[0]=money-sum[1];
}
else
{
buy[0]=0;
}
fstream testbycharfile;
testbycharfile.open("情况.txt",ios::in);
for(int t=0;t<name.length()-1;t++)
{
namebychar[t]=name[t];
}
flag=0;
flag2=0;
flag3=0;
while(!testbycharfile.eof())
{
testbycharfile>>l;
if(flag3==1)
{
if(buy[0]>0)
{
freplacechar("情况.txt",t+1,l-'0'+buy[0]+'0');
}
}
if(flag3==3)
{
freplacechar("情况.txt",t+3,l-'0'+sum[1]+'0');
}
if(flag3==6)
{
freplacechar("情况.txt",t+5,l-'0'+money+'0');
}
if(flag3==8)
{
if(buy[0]<0)
{
freplacechar("情况.txt",t+7,l-'0'+buy[0]+'0');
}
}
if(flag2==1)
{
flag3++;
}
if(l==namebychar[t])
{
t++;
flag=1;
}
else
{
t=0;
flag=0;
}
if(t==name.length()-1)
{
flag2=1;
}
}
if(flag2!=1)
{
cout<<"用户不存在!";
}
}
}
else if(control=="还债")
{
cout<<"请输入昵称"<<endl;
cin>>name;
ifstream filein14("情况.txt",ios::in);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!这里出错
while(getline(filein14,c))
{
length_word[flag]=c.length()-1;
debug(length_word[flag]);
flag+=1;
if(c==name)
{
flag2=1;
break;
}
}
if(flag2==1)
{
fstream testbycharfile;
testbycharfile.open("情况.txt");
t=1;
while(testbycharfile>>l)
{
if(t==length_word[flag3]&&flag4!=1)
{
flag3++;
t=0;
flag--;
}
if(flag==0)
{
flag4=1;
}
if(flag4==1)
{
flag5++;
if(length_word[flag3]-flag5==1)
{
buy[0]=l-'0';
break;
}
else
{
continue;
}
}
t++;
}
if(buy[0]==0)
{
cout<<"没欠钱!";
}
//debug
else if(buy[0]<0)
{
cout<<"[BUG REPORT]"<<endl<<"money less than zero!";
bug(1);
exit(1);
}
else if(buy[0]>7)
{
cout<<"[BUG REPORT]"<<endl<<"own is out of limits!";
bug(2);
exit(2);
}
//enddebug
else
{
cout<<"你欠了"<<buy[0]<<endl<<"请输入已还"<<endl;
cin>>money;
if(money==buy[0])
{
cout<<"已还清!";
freplacechar("情况.txt",length_word[flag3],'0');
}
else if(money>buy[0])
{
cout<<"还的过多!(集数请另外购买)"<<endl;
}
else
{
cout<<"已还"<<buy[0]<<endl<<"还欠"<<buy[0]-money<<endl;
freplacechar("情况.txt",length_word[flag3],money-buy[0]+'0');
}
}
}
else
{
cout<<"昵称不存在!"<<endl;
}
}
cout<<"是否继续?"<<endl;
cin>>ifend;
if(ifend=="否")
{
break;
}
}
return 0;
}
help.h:
#include//bits/stdc++.h
using namespace std;
void debug(int a)
{
cout<<a<<endl;
}
void debug(char a)
{
cout<<a<<endl;
}
void debug(char* a)
{
cout<<a<<endl;
}
void debug(string a)
{
cout<<a<<endl;
}
void freplacechar(string fname,int place,char after_replace_word)
{
char* ch;
char l[1];
l[0]=after_replace_word;
ch=&l[0];
FILE *fp;
fp = fopen(fname.c_str(),"r+");
fseek(fp,place, SEEK_SET);
fwrite(ch,1,1,fp);
fclose(fp);
}
void story()
{
string story;
story=" case'-':\n {\n ofstream fileOut2(\"我的世界Bug日记第----期.txt\",ios::out);\n fileOut2<<----;\n }\n";
}
void bug(int a)
{
ofstream fileout("Bug.log",ios::app);
time_t tt = time(NULL);
struct tm* t= localtime(&tt);
fileout<<t->tm_year + 1900<<"/"<<setw(2)<<setfill('0')<<t->tm_mon + 1<<"/"<<t->tm_mday<<endl<<"["<<setw(2)<<setfill('0')<<t->tm_hour<<":"<<setw(2)<<setfill('0')<<t->tm_min<<":"<<setw(2)<<setfill('0')<<t->tm_sec<<"]"<<endl;
switch(a)
{
case 1:
fileout<<"[BUG REPORT]own money less than zero!"<<endl<<"check the programme or the text"<<endl<<"bugnumber:"<<a<<endl<<"---------------------------------------------------------------------------------------"<<endl;
case 2:
fileout<<"[BUG REPORT]own money is out of limits!"<<endl<<"check the programme of the text"<<endl<<"bugnumber:"<<a<<endl<<"---------------------------------------------------------------------------------------"<<endl;
}
}
while(getline(filein14,c))
{
debug(c);//添加一行打印,看打印出来的text是否是你想的字符串;
length_word[flag]=c.length()-1;
debug(length_word[flag]);
flag+=1;
if(c==name)
{
flag2=1;
break;
}
}
中文乱码问题的话,在写入的时候比如: file << name; 改成 file << name.c_str();