如何低于程序和完美? [英] how to luk good below program and perfect ?

查看:94
本文介绍了如何低于程序和完美?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Login SYstem Program



在这个程序中任何mistec plz告诉



This is Login SYstem Program

In this program any mistec plz tell

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


void userlogin(void);

struct user{
    char username[10];
    char password[10];
}*pUser;

int main()
{
    userlogin ( );

    return 0;
}

void userlogin(void){
    FILE *fp;
    char uName[10], pwd[10];int i;char c;

    pUser=(struct user *)malloc(sizeof(struct user));

    printf("1. Login Through An Existing Account\n2. Create New account\n");
    scanf("%d",& i);
    //system("cls");
    switch(i){
        case 1:
            if ( ( fp=fopen("user.dat", "r+")) == NULL) {
                if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                    printf ("Could not open file\n");
                    exit ( 1);
                }
            }
            printf("Username: ");
            scanf("%9s",uName);
            printf("Password: ");
            scanf("%9s",pwd);
            while ( fread (pUser, sizeof(struct user), 1, fp) == 1) {
                if( strcmp ( pUser-&gt;username, uName) == 0) {
                    printf ("Match username\n");
                    if( strcmp ( pUser-&gt;password, pwd) == 0) {
                        printf ("Match password\n");
                        //accessUser();
                    }
                }
            }
            break;

        case 2:
            do

            {
                if ( ( fp=fopen("user.dat", "a+")) == NULL) {
                    if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                        printf ("Could not open file\n");
                        exit ( 1);
                    }
                }
                printf("Choose A Username: ");
                scanf("%9s",pUser-&gt;username);
                printf("Choose A Password: ");
                scanf("%9s",pUser-&gt;password);
                fwrite (pUser, sizeof(struct user), 1, fp);
                printf("Add another account? (Y/N): ");
                scanf(" %c",&c);//skip leading whitespace
            }while(c=='Y'||c=='y');
            break;
    }
    free ( pUser);//free allocated memory
    fclose(fp);
}

推荐答案

我认为完美的是代码按规范运行时没有任何错误或内存泄漏。这通常需要几轮。



要想到的一件事是尽量使代码尽可能可读。这包括代码的格式和函数中的行数。

对我来说,阅读这样的代码更加困难

I suppose perfect is when the code works according to specification without any bugs or memory leaks. That usually take a few rounds.

One thing to think of is to try to make the code as readable as possible. That includes both the formatting of the code and the number of lines you have in a function.
For me it is more difficult to read code written like this
if ( ( fp=fopen("user.dat", "a+")) == NULL) {
    if ( ( fp=fopen("user.dat", "w+")) == NULL) {
        printf ("Could not open file\n");
        exit ( 1);
    }
}



与此相比


compared to this

if ((fp = fopen("user.dat", "a+")) == NULL)
{
    if ((fp=fopen("user.dat", "w+")) == NULL)
    {
        printf("Could not open file\n");
        exit(1);
    }
}



这当然是品味问题,重要的是保持一致并遵循组织中的任何准则。



这说你的代码中有一些东西可以改进。

1.始终将变量初始化为默认值。


This is of course a matter of taste, and the important thing is to be consistent and follow any guidelines in your organisation.

That said there a couple of things in your code that can be improved.
1. Always initialize variables to a default value.
Such as

FILE *fp = NULL;



1.a.使用calloc而不是malloc


1.a. Use calloc instead of malloc

pUser=(struct user *)malloc(sizeof(struct user));
pUser = (struct user*)calloc(1, sizeof(struct user));



calloc将分配的内存初始化为0 。



2. fopen(user.dat,a +)

如果文件没有,则该语句实际创建该文件存在,所以下一个if语句不是必需的。



3.如果fopen函数失败,你应该检查 errno 变量以获取有关出错的更多信息。



4.您还可以看到在两种情况下使用相同的代码块

所以最好做一个像


calloc initializes the allocated memory to 0.

2. fopen("user.dat", "a+")
This statement actually creates the file if it doesn't exist, so the next if-statement is not necessary.

3. If you the fopen function fails you should check the errno variable for more information about what went wrong.

4. You can also see that you are using the same code block in both cases
So better make a function of like

FILE* OpenFile(const char* fileName)
{
    if ((fp = fopen(fileName, "a+")) == NULL)
    {
        printf("Could not open file: '%s'\n", fileName);
        exit(1);
    }
)





5.添加更多案例时,切换语句往往难以阅读,所以最好为每个案例创建函数。



5. switch statements tend to be difficult to read when you add more cases, so it is better to create functions for each case.

switch(i)
{
    case 1: fp = OpenFile("user.dat");
            OpenAccount();
            break;
    
    case 2: fp = OpenFile("user.dat");
            CreateAccount();
            break;
    
    default: // Always, ALWAYS have a default statement
            printf("This case is not available\n");
            exit(1);
}





6.密码

切勿将密码存储为可读文本。

使用散列算法,如SHA1,并存储散列值。

然后,当用户登录时,您计算他/她输入的密码并与您存储的值进行比较。

参见例如 http://stackoverflow.com/questions/5189257 / sha1-function-in-cpp-c [ ^ ]



这就是我现在能够指出的全部能力。



6. The password
Never store passwords as readable text.
Use a hash algorithm, like SHA1, and store the hashed value instead.
Then when the user login you calculate the password he/she enters and compare with the value you have stored.
See for example http://stackoverflow.com/questions/5189257/sha1-function-in-cpp-c[^]

That was all I had the energy to point out for now.


这篇关于如何低于程序和完美?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆