Whats wrong with fread in this program?

后端 未结 3 1708
-上瘾入骨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条回答
  •  渐次进展
    2021-01-25 12:42

    Problems that I see:

    1. The following line is incorrect.

      scanf("%s",&t_name);
      

      You need to use:

      scanf("%29s",t_name);
      
    2. fread is not the right solution given the file format. fread works when the file is in binary format.

      Given the format of your input file, you need to use:

       scanf("%29s %d", log_in.uname, &log_in.u_pin); 
      
    3. Comparing the pins using log_in.u_pin == t_pin should produce a compiler error. The type of log_in.u_pin is int while the type of t_pin is int [4]. You will have to change your strategy for getting the integer value from t_pin.

      You can use something like:

      int pin = 0; 
      for (i = 0; i < 4; ++i )
      {
         pin = 10*pin + t_pin[i];
      }
      

      However, for that to work, you have store the proper integer values in t_pin. When the character you read is '1', you have to store 1. Either that, or you will have to change the above for loop to:

         pin = 10*pin + t_pin[i]-'0';
      
    4. The way are using goto start to loop in your function is not correct. You haven't read all the user ID and pins to from the input file and compared them. The way you have it, you will end up doing:

      • Read the user name and pin
      • Check against the first entry in the file. If they don't match...

      • Read the user name and pin again.

      • Check against the next entry in the file. If they don't match...

      etc.

      You want to:

      • Read the user name and pin

      • Check against the first entry in the file. If they don't match...

      • Check against the next entry in the file. If they don't match...
      • Check against the next entry in the file. If they don't match...

      etc.

提交回复
热议问题