Whats wrong with fread in this program?

后端 未结 3 1662
-上瘾入骨i
-上瘾入骨i 2021-01-25 12:05

I\'m intermediate student of C. I\'m trying to make a bank management program but first I need to make a login program, so I created one of the following. As I\'ve recently lear

3条回答
  •  -上瘾入骨i
    2021-01-25 12:41

    a) fread is used to read fixed sized (in bytes) records. While that is an option, you might find that fscanf is more suited to your use case.

    b) goto has its uses, arguably, but in your particular case a while loop is far more appropriate as it make the intention clearer and is less susceptible to misuse.

    c) The crux of your issue is reading the file. You only read in and check against a single record from your file per pass and once all records are read you will be getting EOF rather than additional records.

    You have a few options. You might read the entire user table into a list prior to your login loop. When a user logs in you must iterate through the list until a match or end of list is found. Alternatively you might loop through the entire file on each user attempt looking for a match and seek to the back to the beginning of the file when you are done. In either case you must check for and handle read errors.

    d) Finally when a non-digit is entered you will probably find that backspace isn't what you had in mind. Also you might consider allowing the user to press backspace while entering the pin.

提交回复
热议问题