如何比较用户输入的2个密码,然后仅在密码匹配时将其输出到文件 [英] How do I compare 2 passwords inputting by user and then output it to a file only if passwords match
问题描述
> C程序:我需要用户输入他的密码,然后让他再次输入
2匹配程序必须退出并将其保存到文件中。
这是我到目前为止所获得的。
我将不胜感激任何进一步的帮助。
我的尝试:
#include < stdio.h >
#include < stdlib.h > / *对于exit()函数* /
int main()
{
char 用户名[ 1000 ] ;
char passwordone [ 1000 ];
char passwordtwo [ 1000 ];
FILE * fptr;
fptr = fopen( program.txt, w);
if (fptr == NULL)
{
printf( 错误!);
退出( 1 );
}
printf( 输入你的用户名\ n);
得到(用户名);
fprintf(fptr, %s,用户名);
printf( 输入密码\ n);
得到(passwordone);
printf( 再次输入密码\ n);
得到(passwordtwo);
如果 strcmp(passwordone,passwordtwo == 0 )
printf( 密码匹配);
else
printf( 密码与)不匹配;
fprintf(fptr, %s,密码);
fclose(fptr);
return 0 ;
}
快速修复:更改为
引用:如果strcmp(passwordone,passwordtwo == 0)
printf(密码匹配);
else
printf(密码不匹配);
fprintf(fptr,%s,密码);
if (strcmp(passwordone,passwordtwo)== 0 )
{
printf( 密码匹配跨度>);
fprintf(fptr, %s,passwordone);
}
else
printf( 密码不匹配);
请注意:
- 从不使用
得
。请改用fgets
。- 保存明文密码是一种非常糟糕的做法。
修正了一个错误,感谢 Nelek
[update]
Quote:但是我无法了解while循环的概念
您可以这样安排您的程序
#include < string.h >
int main()
{
char 用户名[ 1000 ];
char passwordone [ 1000 ];
char passwordtwo [ 1000 ];
FILE * fptr;
fptr = fopen( program.txt, w);
if (fptr == NULL)
{
printf( 错误!);
返回 - 1 ;
}
printf( 输入你的用户名\ n);
得到(用户名);
fprintf(fptr, %s,用户名);
while ( 1 )
{
printf ( 输入密码\ n);
得到(passwordone);
printf( 再次输入密码\ n);
得到(passwordtwo);
if (strcmp(passwordone,passwordtwo)== 0 )
break ; // 这将退出while循环
printf( 密码与\ n不匹配;
}
printf( 密码匹配\ n);
fprintf(fptr, %s,passwordone);
fclose(fptr);
return 0 ;
[/ update]
if strcmp(passwordone,passwordtwo == 0 )不应该是
if (strcmp(passwordone,passwordtwo)== 0 )
或至少
如果 strcmp(passwordone,passwordtwo) == 0 / * NOT VALID * /
其他一点,你试着写:
fprintf(fptr, %s,密码);
但是
密码
不存在。
其他点,如果你想写它在1和2相同的情况下向下...然后对文件的操作应该在中,如果
,如果它们相同则给出OK
如果它们不匹配,你不应该退出程序,这意味着你必须在迭代/循环中打包你的代码只有在匹配和写入之后才能结束文件。
注意:我们为您提供的工作代码费用不会太高,但目标是您学习。所以请考虑这些线索并更正您的代码。如果再次卡住,请不要犹豫再回来再问。
您是否尝试过编译?
您将收到一些错误消息,指出您应该检查哪些行:
如果strcmp(passwordone,passwordtwo == 0)这显然是无效的C / C ++语法。它必须是
if(strcmp(passwordone,passwordtwo)== 0)
对于这一行,编译器会抱怨关于密码
未申报:
fprintf(fptr,%s,密码);应该很明显如何解决这个问题。
编译器也可能会抱怨没有声明strcmp()
(取决于使用的编译器)。包括 string.h ,其中包含声明。
最后,永远不要使用gets()
功能。请改用fgets()
(但请注意,这不会从输入中删除新行,因此您必须通过代码执行此操作)。
为了满足仅在密码匹配时写入文件的要求,执行写入输出文件,包括相应的中的打开和关闭
在程序结束时阻止。
>C Program : I need the user to input his password and then have him input it once again
only if the 2 match the program must exit and save it into a file.
This is what I have got so far.
Would appreciate any further help on the same.
What I have tried:
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char username[1000];
char passwordone[1000];
char passwordtwo[1000];
FILE *fptr;
fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter your username\n");
gets(username);
fprintf(fptr,"%s", username);
printf("Enter your password\n");
gets(passwordone);
printf("Enter your password again\n");
gets(passwordtwo);
if strcmp(passwordone,passwordtwo ==0)
printf("The Passwords match");
else
printf("The passwords do not match");
fprintf(fptr,"%s", password);
fclose(fptr);
return 0;
}
Quick fix: change from
Quote:if strcmp(passwordone,passwordtwo ==0)
printf("The Passwords match");
else
printf("The passwords do not match");
fprintf(fptr,"%s", password);to
if (strcmp(passwordone,passwordtwo)==0) { printf("The Passwords match"); fprintf(fptr,"%s", passwordone); } else printf("The passwords do not match");
Please note:
- Never, ever use
gets
. Do usefgets
instead.- Saving clear text passwords is a very bad practice.
Fixed a bug, thanks to Nelek
[update]
Quote:However I am not able to understand the concept of the while loop
You may arrange your program this way
#include <string.h> int main() { char username[1000]; char passwordone[1000]; char passwordtwo[1000]; FILE *fptr; fptr = fopen("program.txt", "w"); if(fptr == NULL) { printf("Error!"); return -1; } printf("Enter your username\n"); gets(username); fprintf(fptr,"%s", username); while (1) { printf("Enter your password\n"); gets(passwordone); printf("Enter your password again\n"); gets(passwordtwo); if ( strcmp(passwordone, passwordtwo) == 0) break; // this exits the while loop printf("The passwords do not match\n"); } printf("The Passwords match\n"); fprintf(fptr,"%s", passwordone); fclose(fptr); return 0;
[/update]
Hi,
if strcmp(passwordone,passwordtwo ==0)shouldn't that be
if (strcmp(passwordone,passwordtwo) ==0)
or at least
if strcmp(passwordone,passwordtwo) ==0 /* NOT VALID */
other point, you try to write:
fprintf(fptr,"%s", password);but "
password
" doesn't exist.
other point, if you want to write it down when 1 and 2 are the same... then the actions with the file should go within theif
that gives OK if they are the same
and if they don't match, you should not get out of the program, which means you have to pack your code within an iteration / loop that gets ended only after matching and writing to a file.
Note: For us giving you the working code would cost not that much, but the goal is that you learn. So please think about these clues and correct your code. If you get stuck again, don't hesitate to come back and ask again.
Have you tried to compile it?
You will get some error message indicating which lines you should check:
if strcmp(passwordone,passwordtwo ==0)That is obviously invalid C/C++ syntax. It must be
if (strcmp(passwordone,passwordtwo) == 0)
For this line the compiler complains aboutpassword
being undeclared:
fprintf(fptr,"%s", password);Should be obvious how to fix this.
The compiler might also complain about having no declaration forstrcmp()
(depends on the used compiler). Include string.h which contains the declaration.
Finally, never use thegets()
function. Usefgets()
instead (but note that this will not remove the new line from the input so that you have to do it by code).
To fulfill the requirement of writing to the file only when the passwords match, perform the writing to the output file including the opening and closing in the correspondingif
block at the end of your program.
这篇关于如何比较用户输入的2个密码,然后仅在密码匹配时将其输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!