Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <Windows.h>
- char * createlogin(char filename[50]);
- char * checkusername(char filename[50]);
- char * checkpin(char filename[50]);
- int main(void){
- int done = 0;
- char loginorcreate[7], filename[50];
- while(!done){
- printf("'login' or 'create' new user?: ");
- scanf("%s", &loginorcreate);
- if (strcmp(loginorcreate, "login") == 0){
- checkusername(filename);
- }
- else if (strcmp(loginorcreate, "create") == 0){
- createlogin(filename);
- }
- else{
- printf("exiting...\n");
- return(0);
- done = 1;
- }
- }
- }
- char * createlogin(char filename[50]){
- FILE *userfile;
- char username[20], pin[20];
- printf("Create a username: ");
- scanf("%s", &username);
- sprintf(filename, "C:/programming/users/%s.txt", username);
- if (access (filename, F_OK) != -1){
- printf("Username already exist\n");
- return(0);
- }
- else{
- printf("Create a password: ");
- scanf("%s", &pin);
- userfile = fopen(filename, "w+");
- fprintf(userfile, "%s\n%s\n", username, pin);
- fclose(userfile);
- for(int i = 0; i < 3; i++){
- printf(".");
- Sleep(250);
- }
- printf("\nUser created\n");
- Sleep(500);
- }
- return filename;
- }
- char * checkusername(char filename[50]){
- FILE *userfile;
- char readusername[20], loginusername[20];
- int found_username = 0;
- printf("Username: ");
- scanf("%s", &loginusername);
- sprintf(filename, "C:/programming/users/%s.txt", loginusername);
- userfile = fopen(filename, "r");
- while(fgets(readusername, 20, userfile) != 0){
- if((strstr(readusername, loginusername)) != 0){
- printf("Username found\n");
- checkpin(filename);
- found_username++;
- }
- }
- if (found_username == 0){
- printf("Wrong username\n");
- fclose(userfile);
- return(0);
- }
- }
- char * checkpin(char filename[50]){
- FILE *userfile;
- char pin[20], pininfile[20];
- int found_pin = 0;
- printf("Enter your password: ");
- scanf("%s", &pin);
- userfile = fopen(filename, "r");
- while(fgets(pininfile, 20, userfile) != 0){
- if((strstr(pininfile, pin)) != 0){
- printf("Login succesful\n");
- found_pin++;
- fclose(userfile);
- }
- }
- if (found_pin == 0){
- printf("Wrong password\n");
- fclose(userfile);
- return(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement