Search code examples
cdatabasefilepasswordsfstream

C password database


I've created a program that let's you change the password from a file if you enter the previous password that was in that file.What I want to do is to be able to create a username that's gets assigned with a password.The username and the password should be written in the file without deleting anything that was there before.The program should also be able to validate the password for the username in the file.Here is my current code,but i can't manage to write multiple things in the given file.I don't want you to give me the code for my question, only the algorythm with some tips.Thank you!

#include<stdio.h>
#include <conio.h>
#include <fstream>
#include <string.h>
#include <stdlib.h>     
int main()
{
    FILE *passwords;
    int p='*',i,j,count,triesLeft,a,numberofTries=0;
    char password[5] = {0,0,0,0,0};
    char passwordCheck[5] = {0,0,0,0,0};
    passwords = fopen("passwords.txt","r"); 
    printf("You have 3 tries to enter your password!\n");
    for(count=0;count<3;count++) 
        {
            numberofTries++;
            triesLeft = 3 - count;
            printf("You have %d tries left!\n", triesLeft);
            printf("Enter your password: ");
            scanf("%s", &passwordCheck);
            fscanf(passwords,"%s",&password);
            if(strcmp(password, passwordCheck) == 0)
                {
                    numberofTries--;
                    printf("Press 0 if you want to set up a new password, press 1 to stop the program\n");
                    scanf("%d", &a);
                    if(a==0)
                        {
                            passwords = fopen("passwords.txt","w");
                            printf("New password:");
                            for(i=0;i<5;i++)
                                {
                                    password[i] = getch();
                                    putchar(p); 
                                }
                            for(j=0;j<5;j++)
                                {
                                    fprintf(passwords,"%c",password[j]); 
                                }
                        }
                    else if(a==1)
                        {
                            printf("Old password still in place");
                        }
                    break;
                }
            else
                {
                    printf("Wrong password!");  
                }
        }
    if(numberofTries == 3)
        {
            printf("You are out tries!");
        }
    fclose(passwords); 
}

Solution

  • Have a look at the fopen documentation. The magic phrase here is "access mode". As you open the file, the FILE pointer points to a certain position inside of the file. By setting the appropriate access mode, you can choose where the position pointer will be placed when the file is opened. Maybe the function fseek is interesting for you, too. It allows you to move that pointer.

    A few more tips for your code:

    • Make sure that you don't use to many unneccessary variables, since they make your code confusing.
    • When you use fopen, always check wether the pointer is set correctly (check for NULL), otherwise your program may crash with a segmentation fault if it is unable to find or access the file.
    • In C everything that differs from 0 or NULL is true. That applies for pointers as for numeric values. Feel free to use the negation operator "!" in combination with such tests.