i don understand one thing...
since i declared the array size of the inputted user name is the same as the username inside the file...would that be a problem why would i need BUFSIZE?
Printable View
how are your string functions even working without
#include <string.h>
Or it's not getting exactly three - why not add a debug print to print out the result from scanf() when it's not three. [I find it unlikely that it says more than three].
If you are not getting any warnings if you are not including string.h - you probably aren't using "-Wall" on the compiler line - and thus the warnings aren't enabled, rather than "there aren't any".
--
Mats
A "debug print" is [in this case] just a printf() to print the result variable from your scanf inside the "if ( ... != 3)" side. I think you are capable of doing that without an example - if you are not, you need to go back a few steps and read the first couple of chapters in your C book again.
There is, in large systems, a case of "debug logging", which is generally that the application contains a set of functions, and calls to those functions, to print "useful stuff" when you enable debugging - so you don't have to recompile the application with different code, it already has such code built into it. It usually also invovles different debug levels or debug flags, so you can for example enable logging at "low volume" [debuglevel=1] or "a bit more" [debuglevel=2] or "everything available" [debuglevel=1000]. Or you can have "debug main" [set bit 0 of debugflags] "debug module 1" [set bit 1 of debugflags], "debug module 5", [set bit 5 of debugflags]. If you set debugflags to all ones, you [obviously] get all debug output.
But in your case, the applicaton is small, and easy to change, so just adding and removing the debug output would be the right thing to do.
--
Mats
Guys, this is the latest development i did on my program.
i compiled it in bloodshed C++..no warnings or whatsoever...
but still the line buffer has more than 3 fields...which i think causing the problem...
but i don see where the problem is and why it has more than 3 fields...
can anyone see where i did wrong??
this is just one part of my program, i still have the 2nd and 3rd part...and help would be appreciated cause if haven't settle this first one, i can;t go with the 2 and 3 part.:(
Code:#include<stdio.h>
#include <string.h>
main( )
{
FILE *fpassword;
char line_buffer[100], pass[20], user[20];
fpassword = fopen("C:\\PASSWD.TXT", "r");
if (fpassword == NULL) printf("File doesn't exist\n");
printf("Userid: ");
fgets(user,sizeof(user),stdin);
printf("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 */
{
printf("here's the problem");
getch();
exit(1);
}
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");
getch();
exit(1);
}
else
{
printf("User not authorized");
getch();
exit(1);
}
}
}
}
fclose(fpassword);
getchar();
}
Why don't you move the scanf out of the if, and assign the result to a variable so that you can print the number it actually got, along with the input line that you are trying to scanf()?
--
Mats
Another note: your program doesn't quit if it can't open the file - it simply says an error and continues to execute the program.
hi matxp, i've done what you say...Code:while( fgets(line_buffer,sizeof(line_buffer),fpassword) != NULL ) /* read from file while still data available */
{
char ftemp[20], fpass[20], fuser[20];
printf("%s,%s", user, pass);
sscanf(line_buffer,"%s %s %s",&ftemp,&fpass,&fuser); /* Make sure we parsed 3 fields from the line */
printf("%s,%s,%s\n", ftemp, fpass, fuser);
getch();
exit(1);
}
this is the output...
http://i147.photobucket.com/albums/r...untitled-9.jpg
but earlier before i do scanf i have put these command.
but if it can't open in the first place...shouldn't it terminate instantly?? rather than continue executing...Code:fpassword = fopen("C:\\PASSWD.TXT", "r");
if (fpassword == NULL) printf("File doesn't exist\n");
Doh!
You are passing in string pointers with & - remove the & and you should be fine. [At least I think so]. Since the strings are arrays, they are already passed as the address of the data.
--
Mats
QUOTE=frodonet;683665]but earlier before i do scanf i have put these command.
but if it can't open in the first place...shouldn't it terminate instantly?? rather than continue executing...[/QUOTE]Code:fpassword = fopen("C:\\PASSWD.TXT", "r");
if (fpassword == NULL) printf("File doesn't exist\n");
Well, there's probably no point in continuing, is there? If you DO continue, you will need to check if fpassword is not NULL before you read from it, that's for sure. And what DO you do if you can't read the file?
--
Mats
Because there is a newline at the end of "John1" that is printed as part of the username, then a comma after the username, which comes out on the next line, before the password. You probably want to remove those. I posted a post earlier about how to remove the newline after fgets().
--
Mats