gpt4 book ai didi

C编程登录系统: finding a username

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:54 25 4
gpt4 key购买 nike

好吧,基本上我必须为一个项目制作一个简单的登录系统,我决定为此使用文件,并在这里有一个随机通用帐户的小日志,以及他们的用户名和密码。

Apple Password 
Banana abcdefg
Vader Starwars
Skywalker jedi
Chief Weapon
Gravity Planet
Lightyear long
Hammer nail
Hot-rod car
Speed fast
Shield cover
Tech machine
Pony My_little
Cat Alone
Bro Love
Banshee Ghast

现在我在网上找到了一些程序,它会搜索一个字符串,并告诉我它在哪一行,这样我就可以找到密码,但是当我下载并运行它时,出现了几个问题:

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

//Just some function prototypes.
int Search_in_File(char *str, char *fname);
void Usage(char *filename);

//Our main function.
int main(int argc, char *argv[]) {
int result, errno;

if(argc < 3 || argc > 3) {
Usage(argv[0]);
//exit(1);
getch();
}

//Use system("cls") on windows
//Use system("clear") on Unix/Linux
system("cls");

result = Search_in_File(argv[1], argv[2]);
if(result == -1) {
perror("Error");
printf("Error number = %d\n", errno);
getch();
//exit(1);
}
return(0);
}

void Usage(char *filename) {
printf("fdhjx");
}

int Search_in_File(char *fname, char *str) {
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];

//gcc users
if((fp = fopen("Student Data base.txt", "r")) == NULL)
{
return(-1);
}

/*Visual Studio users
if((fopen_s(&fp, fname, "r")) != NULL) {
return(-1); */
//}
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp, str)) != NULL) {
printf("A match found on line: %d\n", line_num);
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}

if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}

//Close the file if still open.
if(fp) {
fclose(fp);
}
return(0);
}


/* Bonus
/* Below you'll find another way to handle
/* files and error-handling using a stream. *\

//FILE *stream = fopen("test.txt", "r");
//if(!stream) {
/* Handle error properly here */
//return;
//}
//fprintf(stream, "Hello world!");
//fclose(stream);

它编译得很好,但输出崩溃并停止工作,有人能发现问题吗?

最佳答案

启用警告编译告诉我,

警告:函数“getch”的隐式声明

getch 显然是在控制台 I/O header conio.h 中声明的。 getch 不是您例程的核心功能的一部分,因此

  1. 注释掉那些(以及我的 Mac 在运行时无法理解的 system("cls") 行)和

  2. 提供两个命令行参数

给出一个工作例程:

./zpa fakefilename Sky
A match found on line: 4

Skywalker jedi

请注意,Search_in_Filefname 参数未被使用,因此命令行输入可以减少为一个参数。将借用的代码减少到您需要的最小核心通常对调试非常有帮助。

关于C编程登录系统: finding a username,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21310772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com