Thread: Compare a password in char and struct

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Compare a password in char and struct

    Hi!

    Im writing on a program where a user will log in. The user enters a username and a password in a struct that is saved in a binaryfile on the harddrive.

    When the user wants to login im thinking that the binaryfile will be read with fread and the user writes his password into an char array.

    Then i would like to compare with the following:

    char password == file->password

    But this doesnt work! Even if the passwords are the same the expression is false.

    Can someone explain why and explain if I could do this in another way? Is there a \n or \0 that im forgetting to check?

    /Roger

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    There could be several reasons for this issue. Post a complete (preferably simplified) version of the program that exhibits the problem.

    Quote Originally Posted by roger82 View Post
    Then i would like to compare with the following:

    char password == file->password
    To compare strings in C, you should be using strcmp.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    Of course, strcmp! That solved it, why did that fell out of the head? Must be tired.....

    Im greatful for the help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare pointer to struct and struct
    By thachdovan in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2016, 10:23 PM
  2. Struct compare
    By John5 in forum C Programming
    Replies: 7
    Last Post: 03-06-2016, 07:12 PM
  3. Compare unsigned char array with const char*
    By Lazar in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2015, 08:36 AM
  4. Compare Struct Variables
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 12:06 PM
  5. how to compare char array with one char?
    By Jasonymk in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2003, 09:49 AM

Tags for this Thread