Thread: Problem I can't seem to solve

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    Why binary mode for strings? If you really need binary mode, shouldn't you use fread instead of fgets?

    Do you really need clrscr and conio.h?

    [edit]
    Code:
    fgets(temp_user,sizeof(user)+1,login);
    Read one less than the string can hold (even when accounting for the null character), but disguise it to look like it's reading one more than it should?

    Kinda the same thing here:
    Code:
    scanf("%15s", user);
    What's \i?
    Code:
    else printf("\incorrect\n");
    Is login a FILE* or a function?
    Code:
    int login(void){
    
    char user[15];
    char pass[15];
    char temp_user[17];
    char temp_pass[17];
    int sw_ok=1;
    FILE *login;
    Last edited by Dave_Sinkula; 05-15-2005 at 04:09 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    3
    Quote Originally Posted by Dave_Sinkula
    Why binary mode for strings? If you really need binary mode, shouldn't you use fread instead of fgets?
    I use binary because a txt file would enable users to go look into the txt file. Ok one could say they just have to open the file in notepad to see it too.... but not everyone knows that. + I still need to find a way to "encrypt" the writing to a file.


    thanks for the feedback, I'll change the loop with feof like the tutorial explains...

    here are the answers to your questions:

    Quote Originally Posted by Dave_Sinkula
    Do you really need clrscr and conio.h?
    yes I do... before entering this function, the screen needs to be cleared because they already got an intro function with a header, menu ...
    anyway this is just esthetics...

    Quote Originally Posted by Dave_Sinkula
    [edit]
    Code:
    fgets(temp_user,sizeof(user)+1,login);
    Read one less than the string can hold (even when accounting for the null character), but disguise it to look like it's reading one more than it should?
    well this is odd I must admit... but without the +1 the watch tells me it reads in "[blank spaces]... user" (it omits the last character.. here a 1 )
    by doing the +1 it reads the info correctly...: "[blankspaces]....user1"




    Quote Originally Posted by Dave_Sinkula
    Kinda the same thing here:
    Code:
    scanf("%15s", user);
    I put this because I made a limit that max. 15 characters are to be used for login and password...

    + I was hoping by putting this like that... when a user enters for instance user1

    it would store it like this " user1"

    thus the strcmp between this and the sequence read out of the file would be equal
    and thus result in "login ok"



    Quote Originally Posted by Dave_Sinkula
    What's \i?
    Code:
    else printf("\incorrect\n");
    sorry typo after having paste it into this box


    Quote Originally Posted by Dave_Sinkula
    Is login a FILE* or a function?
    Code:
    int login(void){
    
    char user[15];
    char pass[15];
    char temp_user[17];
    char temp_pass[17];
    int sw_ok=1;
    FILE *login;


    good point...
    I renamed the function to "inloggen" (dutch)

    this function is called out from the main function..



    thanks for the feedback...

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by psykik
    I put this because I made a limit that max. 15 characters are to be used for login and password...
    And this doesn't account the null character.

    Food for thought...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *line, int size, const char *prompt)
    {
       fputs(prompt, stdout);
       fflush(stdout);
       if ( fgets(line, size, stdin) )
       {
          char *newline = strchr(line, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
       }
       return line;
    }
    
    int main(void)
    {
       char file_user[15] = "         user1";
       char file_pass[15] = "         pass1";
       char user [ sizeof file_user + 1/* expect trailing newline with fgets*/ ];
       char pass [ sizeof file_pass + 1/* expect trailing newline with fgets*/ ];
       static const char filename[] = "users.dat";
       FILE *file = fopen(filename, "wb");
       if ( file )
       {
          fwrite(file_user, sizeof file_user, 1, file);
          fwrite(file_pass, sizeof file_pass, 1, file);
          fclose(file);
       }
       foo(user, sizeof user, "username: ");
       foo(pass, sizeof pass, "password: ");
       file = fopen(filename, "rb");
       if ( file )
       {
          while ( fread(file_user, sizeof file_user, 1, file) == 1 &&
                  fread(file_pass, sizeof file_pass, 1, file) == 1 )
          {
             if ( strcmp(file_user, user) == 0 && strcmp(file_pass, pass) == 0 )
             {
                puts("Access OK");
                goto done;
             }
          }
          puts("Invalid Access");
          done:
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    username: user1
    password: pass1
    Invalid Access
    
    username:          user1
    password:          pass1
    Access OK
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    3
    thanks for the code and alternative way...

    I've been looking into how to do it also
    and came up with this:


    (sorry if the code is lame... I'm still quite new at C)
    also the printf's are in dutch...



    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int inloggen(void){
    
    char user[15];
    char pass[15];
    char **temp_user,**temp_pass,lijn[31],c='o'; //15+15+1
    //int's voor de lussen...
    int sw_ok=1, sw_user=1, sw_pass=1,sw_gevonden=0,lengte=0,i=0;
    FILE *login;
    
    
    printf("\t**********************************************\n");
    printf("\t*                  Login                     *\n");
    printf("\t**********************************************\n");
    
    login=fopen("users.dat", "rb");
    fscanf(login,"%*s");
    	 while(!feof(login))
    	 {	  lengte++;
    		  fscanf(login,"%*s");
    	 }
    	 rewind(login);
    	 temp_user=calloc(sizeof(char*),lengte);
    	 temp_pass=calloc(sizeof(char*),lengte);
    	 for(i=0;i<lengte;i++)
    	 {   fscanf(login,"%s",lijn);
    		  temp_user[i]=malloc(16);
    		  strncpy(temp_user[i],lijn,15);
    		  fscanf(login,"%s",lijn);
    		  temp_pass[i]=malloc(16);
    		  strncpy(temp_pass[i],lijn,15);
    	 }
    	 fclose(login);
    
    while (sw_ok)
    {
    	while(sw_user)
    	{
    		printf("Gebruikersnaam: ");
    		scanf("%15s%c", user,&c);
    		if(c!='\n')
    		{
    			printf("Deze gebruikersnaam is te lang. U kan enkel een gebruikersnaam gebruiken van 15 karakters.\n");
    			scanf("%*[^\n]%*c");
    		}
    		else sw_user=0;
    	}
    
    	while(sw_pass)
    	{
    		printf("Geef uw paswoord: ");
    		scanf("%15s%c",pass,&c);
    		if(c!='\n')
    		{
    			printf("Foutief paswoord...(te lang, max 15 karakters)\n");
    			scanf("%*[^\n]%*c");
    		}
    		else sw_pass=0;
    	}
    
    
    for(i=0;i<lengte;i++){
    	if (strcmp(temp_user[i], user) == 0 && strcmp(temp_pass[i], pass) ==0){
    							sw_ok=0;
    							sw_gevonden=1;
    				}
    				else  {
    					sw_user=1;
    					sw_pass=1;
    					}
    	 }//einde for lus
    if (sw_gevonden)
    {
    printf("***********************\n");
    printf("*     Toegang OK      *\n");
    printf("***********************\n");
    menu();
    }
    else printf("Foute gebruikersnaam of paswoord\nProbeer opnieuw AUB\n");
    }
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 12:31 PM
  3. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  4. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 03:23 PM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM