Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <cs50.h>
- #include <ctype.h>
- int main (void) {
- char jop=get_char("Find user <f> Add user <a>\n");
- jop=tolower(jop);
- FILE *user;
- /********************************Find user***********************/
- if(jop == 'f'){
- char *u_name=get_string("Username: ");
- char *u_inf=malloc(strlen(u_name)+5);
- strcpy(u_inf, u_name);
- sprintf(u_inf,"%s.txt",u_name);
- //read file
- user=fopen(u_inf,"r");
- //Check if User is null
- if(user == NULL){
- fprintf(stderr,"Couldn't find user\n");
- fclose(user);
- return 2;
- }
- //Get data from file
- fread(u_inf,strlen(u_name)+19,1,user);
- printf("Password: %s\n",u_inf);
- fclose(user);
- return 0;
- /********************************Add user***********************/
- }
- if(jop == 'a'){
- char *u_name=get_string("Username: ");
- char *u_inf=malloc(strlen(u_name)+5);
- strcpy(u_inf, u_name);
- sprintf(u_inf,"%s.txt",u_name);
- //Write and read file
- user=fopen(u_inf,"w+");
- //Check if User was null
- if(user == NULL){
- fprintf(stderr,"Couldn't add user\n");
- fclose(user);
- return 3;
- }
- //Get password from user
- string u_password=get_string("Password: ");
- //Check if password was null
- if(u_password == NULL){
- fprintf(stderr,"Couldn't get password\n");
- fclose(user);
- return 4;
- }
- //Check if password wasn't more than 13
- if(strlen(u_password)>13){
- fprintf(stderr,"Error password max lenth is 13\n");
- fclose(user);
- return 5;
- }
- sprintf(u_inf,"%s",u_password);
- //write file
- fwrite(u_inf,strlen(u_inf)+1,1,user);
- fseek(user,strlen(u_inf)+1,SEEK_SET);
- fprintf(stderr,"file created successful\n");
- fclose(user);
- return 0;
- }
- if(jop!='a'|| jop!='f'){
- fprintf(stderr,"Error\n");
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement