Last active
May 24, 2024 02:31
-
-
Save zxmarcos/4731109 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #define LOGIN_MAX 64 | |
| #define SENHA_MAX 32 | |
| struct usuario_t { | |
| char login[64]; | |
| char senha[32]; | |
| }; | |
| void cadastrar() | |
| { | |
| FILE *fp; | |
| struct usuario_t usuario; | |
| memset(&usuario, 0, sizeof(struct usuario_t)); | |
| printf("Cadastro de usuário: \n"); | |
| printf("login> "); | |
| scanf("%s", &usuario.login); | |
| printf("senha> "); | |
| scanf("%s", &usuario.senha); | |
| fp = fopen("login", "wb"); | |
| if (!fp) { | |
| printf("Não foi possível criar o arquivo de login"); | |
| return; | |
| } | |
| fwrite(&usuario, sizeof(struct usuario_t), 1, fp); | |
| fclose(fp); | |
| } | |
| int login() | |
| { | |
| FILE *fp; | |
| struct usuario_t usuario; | |
| fp = fopen("login", "rb"); | |
| if (!fp) { | |
| cadastrar(); | |
| fp = fopen("login", "rb"); | |
| if (!fp) { | |
| printf("Não foi possivel encontrar o arquivo de login\n"); | |
| return 0; | |
| } | |
| } | |
| memset(&usuario, 0, sizeof(struct usuario_t)); | |
| fread(&usuario, sizeof(struct usuario_t), 1, fp); | |
| fclose(fp); | |
| if (!strcmp("", usuario.login)) { | |
| printf("Nenhum usuário encontrado, abrindo cadastro!\n"); | |
| cadastrar(); | |
| } else { | |
| int tentativas = 0; | |
| do { | |
| char login[LOGIN_MAX]; | |
| char senha[SENHA_MAX]; | |
| printf("Login: "); | |
| scanf("%s", &login); | |
| printf("Senha: "); | |
| scanf("%s", &senha); | |
| if (!strcmp(login, usuario.login) && !strcmp(senha, usuario.senha)) { | |
| printf("Bem vindo %s\n", usuario.login); | |
| return 1; | |
| } else { | |
| printf("Usuário ou senha invalidos!\n"); | |
| tentativas++; | |
| } | |
| } while (tentativas < 3); | |
| printf("Numero maximo de tentativas alcançado!\n"); | |
| return 0; | |
| } | |
| return 0; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| login(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment