String comparison.

This is a discussion on String comparison. within the C Programming forums, part of the General Programming Boards category; Code: int compare_masterpw(const char* password) { char pas; FILE* fp = fopen("conf/bypass.txt", "r"); ShowInfo("Reading file conf/bypass.txt...\n"); if (fp == NULL) ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    String comparison.

    Code:
    int compare_masterpw(const char* password)
    {
    	char pas;
    	FILE* fp = fopen("conf/bypass.txt", "r");
    
    
    	ShowInfo("Reading file conf/bypass.txt...\n");
    	if (fp == NULL) {
    		ShowError("Could not load password bypass file! (conf/bypass.txt)\n");
    		return 0;
    	}
    	ShowInfo("Done reading conf/bypass.txt.\n");
    
    	fgets(pas,100,fp);
    	fclose(fp);
    
    	ShowImportant(pas,"\n");
    	ShowImportant(password,"\n");
    
    	if(pas == password)
    	{
    		ShowImportant("Good\n");
    		return 1;
    	}
    	else
    	{
    		ShowImportant("Bad\n");
    		return 0;
    	}
    }
    When I compile, I get these errors:

    login.c:237: warning: implicit declaration of function ‚compare_masterpw‚
    login.c: In function ‚compare_masterpw‚:
    login.c:1530: warning: passing argument 1 of ‚fgets‚ makes pointer from integer without a cast
    login.c:1533: warning: passing argument 1 of ‚ShowImportant‚ makes pointer from integer without a cast
    login.c:1536: warning: comparison between pointer and integer
    login.c:1530: warning: ‚pas‚ is used uninitialized in this function
    Any idea how I might fix this?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    char pas;
    
    ...
    
    fgets(pas,100,fp);
    
    ...
    
    if(pas == password)
    First off, it looks like you're trying to read 100 characters into a buffer that's only got space for a single character. Your "pas" variable needs to be made into an array.

    Second, you can't compare C-style strings (null terminated char arrays) using the equality operator ==. The above code will only end up comparing the addresses of the two arrays which are NEVER going to be equal. You need to use the strcmp function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21