1.登录账号密码
void sign_in(FILE *fp)//注册
{
char usrname[10];
char pwd[12];
printf("输入账户:");
scanf("%s",usrname);
printf("输入密码:");
scanf(" %s",pwd);
fprintf(fp,"%s %s\n",usrname,pwd);
printf("注册成功\n");
}
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./information.txt","a+");
/*sign_in(fp);
sign_in(fp);
sign_in(fp);
fclose(fp);
*/ char usrname[10];
char pwd[12];//文件中的用户密码
char Uname[10];
char Pword[12];//要登录的用户密码
fp=fopen("./information.txt","r");
printf("输入账户:");
scanf("%s",Uname);
printf("输入密码:");
scanf("%s",Pword);
int j=-1;
while(fscanf(fp,"%s %s",usrname,pwd)!=EOF)
{
if(strcmp(usrname,Uname)==0&&strcmp(pwd,Pword)==0)
{
printf("登陆成功\n");
j=0;
break;
}
else if(strcmp(usrname,Uname)==0&&strcmp(pwd,Pword)!=0)
{
printf("密码错误\n");
j=1;
break;
}
}
if(j!=0&&j!=1)
{
printf("账户不存在\n");
}
fclose(fp);
return 0;
}
2.计算文件字符数,行数
#include <myhead.h>
void count(FILE *fp)
{
int count1=0;
int count2=0;
while(1)
{
char c=fgetc(fp);
if(c==EOF)
{
break;
}
count1++;
if(c=='\n')
{
count2++;
}
}
printf("字符个数=%d\n",count1);
printf("行数=%d\n",count2);
}
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./1.txt","r");
count(fp);
fclose(fp);
return 0;
}
3.xmind