比较C,DevC ++中的字符串 [英] Compare Strings in C, DevC++
本文介绍了比较C,DevC ++中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想建立一个文件管理系统,但我需要一些帮助。我似乎无法弄清楚如何实际比较C中的字符串。这是我目前的代码:
I wanted to make a file management system but I need some help with it. I can't seem to figure out how to actually compare strings in C. Here is my current code:
# include <stdlib.h> // for exit()
# include <stdio.h> // defines FILE
//project task
//ask for username and password of user.
//make 2 array (1 for username, 1 for password)
int pp;
//global list
FILE *fptr; // ptr to file for username
char ch;
FILE *x;
FILE *fptr2; // ptr to file for password
char ch2;
FILE *z;
char theString[] = "First Mid Last";
char theString1[100];
char theString2[100];
char zzzz[100];
char storeuser[100];
//global list
int main()
{
if ((fptr = fopen("username.txt","r"))== NULL ) // open file in writing mode
{
printf("Can't open file username.txt\n");
getch();
exit(1); // error return value
}
if ((fptr2 = fopen("password.txt","r"))== NULL ) // open file in writing mode
{
printf("Can't open file password.txt\n");
getch();
exit(1); // error return value
}
printf("Enter your userame\n");
scanf("%s", &theString1);
// printf("%s", theString1);
printf("Enter your password\n");
scanf("%s", &theString2);
// printf("%s", theString2);
printf("%s", fptr);
while ((ch = getc(fptr)) != EOF)
printf("%c",ch);
fclose(fptr);
getch();
return 0;
}
我想将theString1与username.txt中的文本进行比较,该文本正在使用fptr变量。
任何有关此问题的帮助都会被批评:)
谢谢。
I want to compare theString1 with the text in username.txt which is being accessed using the fptr variable.
Any help regarding this would be appricated :)
Thanks.
推荐答案
您的输入文件可能由多行组成,每行包含用户名和密码(可能用哈希函数编码)。因此,在username.txt文件中的行上执行循环。在每个循环迭代中:
a)从该行中提取用户名和密码
b)将用户名与theString1中的控制台输入进行比较。
你可以使用cmpstr或cmpistr(用于不区分大小写的比较)来比较theString1中的字符串和用户名。
Your input file does probably consist of multiple line, each containing a username and password (probably encoded with a hash function). So perform a loop over the lines in your username.txt file. In each loop iteration:
a) extract the username and password out of that line
b) compare the username with the console input in theString1.
You may use the cmpstr or cmpistr (for case-insensitive comparison) to compare the string in theString1 with the username.
这篇关于比较C,DevC ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文