The logic is like this:
Read one line from file, store in line_buffer.
Condition for loop is if the read succeeds. In other words, if the read fails, the loop breaks.
Then you break the data into separate variables.
You check the user and fuser strings to see if they're equal. If yes, then you check if the pass and fpass is equal. If yes, then print a message and wait for a keypress.
Then continue the loop, since you haven't entered any break or return.
Loop.
That's the code I've already presented to you. No need to do it again.
Not mad. Just telling you where the problem lies
You should remember that code execution always continues forward unless you tell the compiler it shouldn't. To do that, you can use break (breaks a loop), continue (jumps to the beginning of the loop again) or return (exits the function).
There's also goto, but many consider it bad. Goto jumps to a specific place in your code. Take care when using it, though, since it can really make your code look messy.
Last edited by Elysia; 10-30-2007 at 02:22 PM.
arghhh... the program still closes after i pressed userid and password.Code:#include<stdio.h> main( ) { FILE *fpassword; char c, line_buffer[100], user[20], pass[5]; fpassword = fopen("C:\\PASSWD.TXT", "r"); if (fpassword == NULL) printf("File doesn't exist\n"); printf("Enter userid: "); fgets(user,sizeof(user),stdin); printf("Enter password: "); fgets(pass,sizeof(pass),stdin); while( fgets(line_buffer,sizeof(line_buffer),fpassword) != NULL ) /* read from file while still data available */ { char ftemp[20], fpass[20], fuser[20]; if( sscanf(line_buffer,"%.20s %.20s %.20s",ftemp,fpass,fuser) != 3 ) /* Make sure we parsed 3 fields from the line */ { /* Invalid line in file, skip to next one */ } else /* Otherwise, we got 3 fields */ { if (strcmp(user, fuser) == 0) /* OK, found the username the user inputted */ { if (strcmp(pass, fpass) == 0) /* Now, check if passwords match, if they do not, user entered a faulty password */ { printf("User authorized"); getchar(); } else { printf("User not authorized"); getchar(); } } } } fclose(fpassword); getchar(); }
i just couldn't figure it out help me
5 chars for a password?
Since one of them will be \0, and the other one will be a \n (for a complete line), that leaves you with 3 chars.
Type any more than that, and you're out of sync in the rest of the program.
With fgets(), use an array of BUFSIZ characters, then validate/process that buffer to it's correct variable in your program.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Validate in this case refers to checking the name/password.
--
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.
BUFSIZ is just a constant that is defined to be some value specific to the compiler/system.
Well, in this case, validation could also be considered "validating that the input is correct".
For example, if you use fgets() to get a short string, and someone types a longer string, the next fgets() will get the remaining data from the input buffer.
The input buffer is BUFSIZ long, so we can't get more data than a BUFSIZ at a time [most likely].
--
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.
Well, the "example" would depend on what you need to do in "validating the input". For example, if you are asking for a username that is supposed to be 20 chars long, then you may do something like:
--Code:char temp[BUFSIZ]; char username[21] = "!!InvalidUser!!"; // Make a default name that can't be used printf("Username: "); if (fgets(temp, BUFSIZ, stdin) != NULL) { // Remove newline if it's there. if (temp[strlen(temp)-1] == '\n') temp[strlen(temp)-1] = 0; if (strlen(temp) > sizeof(username)-1) { /// username is too long - tell user, or just ignore it [leaving the "default" invalid name in there]. } else { strcpy(username, temp); } } ...
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.