簡體   English   中英

C密碼數據庫

[英]C password database

我創建了一個程序,如果您輸入該文件中的先前密碼,則可以從該文件中更改密碼。我想要做的是能夠創建一個分配了密碼的用戶名。密碼應該寫在文件中而不刪除以前存在的任何內容。該程序還應該能夠驗證文件中用戶名的密碼。這是我當前的代碼,但是我無法在其中寫很多東西給定的文件。我不想讓我給我的問題提供代碼,只給我一些提示的算法。謝謝!

#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); 
}

看一下fopen文檔 這里的魔術短語是“訪問模式”。 當您打開文件時,FILE指針指向文件內部的某個位置。 通過設置適當的訪問模式,您可以選擇打開文件時位置指針的放置位置。 也許fseek函數對您來說也很有趣。 它允許您移動該指針。

有關代碼的更多提示:

  • 確保不要使用很多不必要的變量,因為它們會使您的代碼混亂。
  • 使用fopen時,請始終檢查是否正確設置了指針(檢查NULL),否則,如果程序無法找到或訪問文件,則程序可能會因分段錯誤而崩潰。
  • 在C語言中,所有不同於0或NULL的值都為true。 這適用於指針和數值。 隨意使用否定運算符“!” 結合這樣的測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM