Thread: error checking file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    26

    error checking file

    Right newbie here! Im am tyring to write some code that will read a file i already have and check a login name against what is on file. Then i need it to give an error warning if the name is not there.

    I have started this but i havent been able to get it to work. Any help would be much appreciated!

    Code:
    int Login(int* user)
    {
      char str [80];
      int h;
      
      
      FILE * db;
    
      db = fopen ("text.txt","a+");
      
    	printf("Please enter login name: ");
      	
    	scanf("%s", &h); 
      	
    	fscanf (db, "%s", &user);
    	
    	if (h != "user") printf("Access Denied! Please Register."); 
      	
    	fclose (db);
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    i think
    scanf("%s", &h) is not correct. if you want to store a string (as it seems) you should not use the & operator. And in that case you should declare h as char * h for example.
    maybe...
    Mac OS 10.6 Snow Leopard : Darwin

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Also, the fscanf is wrong for the same reason and thus probably also the function's user argument.

    Second, this:
    Code:
    if (h != "user") ...
    ...isn't going to work. You need to use strcmp otherwise this bit of code is simply going to try and compare the address' of these two variables which are never going to be equal to each other.

    And why use "a+" instead of something like "r" when opening the file?
    Last edited by hk_mp5kpdw; 01-28-2008 at 12:19 PM.
    "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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nacho4d View Post
    i think
    scanf("%s", &h) is not correct. if you want to store a string (as it seems) you should not use the & operator. And in that case you should declare h as char * h for example.
    maybe...
    No. That will cause even more problems. You need to allocate memory first. So use a string array. And it's probably better to use fgets to read a string from input.
    Nexus-ZERO, I recommend you read these resources:
    http://cboard.cprogramming.com/showp...37&postcount=9
    http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok ive change my based on the suggestions i got. I just need to know how to set it so that it only accepts the user names in my txt file. (change the indentation once i get it working!)

    Code:
    int Login(int* user)
    {
      char s[80];
      
      FILE * db;
    
      db = fopen ("text.txt","r");
      
    	printf("Please enter your login: ");
    	gets(s);
    	
    	if(strcmp(s, "pass")) {
    	 printf("\n\nInvalid Login!\n\n");
    	 return 0;
    	 
    	}
    	
    	return 1;
    	
    	fclose (db);
    }
    Last edited by Nexus-ZERO; 01-28-2008 at 02:16 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The function called "gets" is a very bad function - there is no protection if the user types more than 80 characters, and your application will most likely crash if this happens - try hanging on the "a" key for a bit and see what happens.

    Code:
    fgets(s, sizeof(s), stdin)
    is a much more safe function. Just remember to throw this little bit of code after it:
    Code:
    if (s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    I changed it but now it just skips throught the login bit and back to the main menu. Does anyone know how to stop it doing this.

    Code:
    int Login(int* user)
    {
      char s[80];
      
      FILE * db;
    
      db = fopen ("text.txt","r");
      
    	printf("Please enter your login: ");
    	fgets(s, sizeof(s), stdin);
    	
    	if (s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0;
    	
    	if(strcmp(s, "pass")) {
    	 printf("\n\nInvalid Login!\n\n");
    	 
    	 //return 0;
    	}
    	
    	//return 1;
    	
    	fclose (db);
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess - you have done something else first, that involves scanf()?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Yeah i vaguely remember being there lol

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can't explain why it's working different from gets(), but you are getting a "newline" (enter key) in the input buffer from scanf(), so you need to read that out of the input buffer before calling fgets().

    A simple "getchar()" after the last of your scanf() [last one before calling "Login" that is] should do the trick.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    That worked thank you, but it still hasnt solved my main dilemma. How do get it to only accept user name that i have on my file?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Assuming your file looks like

    user
    pass
    user
    pass

    Then you could just read line-by-line (skipping the password line) and comparing the entered user with the one from the file. If you can match one of them, then it's valid, but if not, it's invalid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM