这是什么情况,有哪位大神指点一下

Tenacious0 2018-07-26 08:23:08
...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zangfong 2018-07-28
  • 打赏
  • 举报
回复
sscanf 从指定字符串输入,而fscanf 是从文件输入,下面是fscanf的应用
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int main(void)
{
FILE *file;
char name[123];
char password[123];
char name_tmp[123];
char password_tmp[123];
char name_pass[256];
char *ret;
int login_success = 0;

file = fopen("D:\\liu\\lx.txt","r+");
if (file == NULL)
{
printf("文件打开失败!");
return 1;
}

while(1)
{
printf("请输入用户名:");
scanf("%s",name);
printf("请输入密码:");
scanf("%s",password);

while(1)
{
// ret = fgets(name_pass,sizeof(name_pass),file);
// if (ret==NULL) //判断文件有没有读取成功
// {
// break;
// }
//
// sscanf(name_pass,"%s %s",name_tmp,password_tmp);

fscanf(file,"%s %s",name_tmp,password_tmp);

if((strcmp(name,name_tmp) == 0) && strcmp(password,password_tmp) == 0)
{
printf("登陆成功!\n");
break;
}
else
{
printf("用户名或密码错误!");
system("pause");
system("cls");
break;
}
}
}
fclose(file);
return 0;
}

zangfong 2018-07-28
  • 打赏
  • 举报
回复
你的第12行
char name_pass;
应该是想定义成类似如下的字符数组的吧
    char name_pass[256];

这么修改,再加上头文件
#include<stdlib.h>
至少你贴出来的代码可以运行了,如下
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int main(void)
{
FILE *file;
char name[123];
char password[123];
char name_tmp[123];
char password_tmp[123];
char name_pass[256];
char *ret;

file = fopen("D:\\liu\\lx.txt","r+");
if (file == NULL)
{
printf("文件打开失败!");
return 1;
}

while(1)
{
printf("请输入用户名:");
scanf("%s",name);
printf("请输入密码:");
scanf("%s",password);

while(1)
{
ret = fgets(name_pass,sizeof(name_pass),file);
if (ret==NULL) //判断文件有没有读取成功
{
break;
}

sscanf(name_pass,"%s %s",name_tmp,password_tmp);

if((strcmp(name,name_tmp) == 0) && strcmp(password,password_tmp) == 0)
{
break;
}
else
{
printf("用户名或密码错误!");
system("pause");
system("cls");
}
}
}
return 0;
}
Tenacious0 2018-07-28
  • 打赏
  • 举报
回复
好的谢谢。在问一下,fscanf和sscanf的语法有什么区别
自信男孩 2018-07-27
  • 打赏
  • 举报
回复
能否把代码直接贴出来,测试现象可以截图嘛
Tenacious0 2018-07-27
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<string.h>

int main(void) {
FILE *file;
char name[123];
char password[123];
char name_tmp[123];
char password_tmp[123];
char name_pass;
char *ret;

file = fopen("D:\\liu\\lx.txt","r+");
if (file == NULL) {
printf("文件打开失败!");
return 1;
}

while(1) {
printf("请输入用户名:");
scanf("%s",name);
printf("请输入密码:");
scanf("%s",password);

while(1) {
ret = fgets(name_pass,sizeof(name_pass),file);
if (ret==NULL) { //判断文件有没有读取成功
break;
}

sscanf(name_pass,"%s %s",name_tmp,password_tmp);

if((strcmp(name,name_tmp) == 0) && strcmp(password,password_tmp) == 0) {
break;
} else {
printf("用户名或密码错误!");
system("pause");
system("cls");
}
}
}
return 0;


下面是编译错误代码
aaa.c: In function 'main':
aaa.c:26:4: warning: passing argument 1 of 'fgets' makes pointer from integer without a cast [enabled by default]
In file included from aaa.c:1:0:
c:\mingw\bin\../lib/gcc/mingw32/4.7.1/../../../../include/stdio.h:354:39: note: expected 'char *' but argument is of type 'char'
aaa.c:31:4: warning: passing argument 1 of 'sscanf' makes pointer from integer without a cast [enabled by default]
In file included from aaa.c:1:0:
c:\mingw\bin\../lib/gcc/mingw32/4.7.1/../../../../include/stdio.h:348:37: note: expected 'const char *' but argument is of type 'char'
aaa.c:42:2: error: expected declaration or statement at end of input

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧