Problem in file handling.

This is a discussion on Problem in file handling. within the C Programming forums, part of the General Programming Boards category; Hello, I am a new member.In my college we are to make a program to store usernames and passwords in ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    India
    Posts
    2

    Post Problem in file handling.

    Hello,
    I am a new member.In my college we are to make a program to store usernames and passwords in a file, and when a user enters a correct username and password, "authentication" must be granted to him.But,in my code, even though on entering the correct username and password, there is no authentication.
    Here is the code :
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct records
    {
    char usr[10];
    char pass[15];
    }list;
    void main()
    {
    char filename[10];
    int res,flag=0;
    char username[10],password[15];
    FILE *fp;
    long n,q;
    struct records *p;
    p=&list;
    clrscr();
    fp=fopen("Rec","a+");
    strcpy(list.usr,"nitin");
    strcpy(list.pass,"kasbekar");
    fprintf(fp,"%s %s",p->usr,p->pass);
    strcpy(list.usr,"rahul");
    strcpy(list.pass,"dixit");
    fprintf(fp,"%s %s",p->usr,p->pass);
    strcpy(list.usr,"abhishek");
    strcpy(list.pass,"dwivedi");
    fprintf(fp,"%s %s",p->usr,p->pass);
    n=ftell(fp);
    fclose(fp);
    fp=fopen("Rec","r");
    printf("Enter username ");
    scanf("%s",username);
    printf("Enter password ");
    scanf("%s",password);
    printf("\n%d\n",n);
    while(ftell(fp)<n)
    {
    fscanf(fp,"%s %s",list.usr,list.pass);
    //q=ftell(fp);
    if(strcmp(list.usr,username)==1&&strcmp(list.pass,password)==1)
    {
    printf("\n %d ",ftell(fp));
    flag=1;
    break;
    }
    else
    {
    continue;
    }}
    if(flag==0)
    printf("Incorrect username/password ");
    else
    printf("User authenticated ");
    fclose(fp);
    getch();
    }
    If any member is able to help me, please do.
    Thank you.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    You need to indent your code, what you have there is a bad indentation style.

    Code:
    	void main()
    this is wrong.
    Last edited by InvariantLoop; 02-18-2005 at 08:52 AM.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    no problem with your logic i guess..
    but,
    1) as invariantloop pointed out, your indentation is horrible.
    2) main returns an int. wait till salem posts, watch his avataar, you'l know why. it should be int main() and not void main()
    3)strcmp returns 0 and not 1 if the strings are the same. i guess thats where your problem lies. check for
    Code:
    strcmp()==0
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    India
    Posts
    2

    Post Problem persists

    Hello,
    I thank Ping and invariantloop for their suggestions.I made the 1's to 0's in the if condition,but the code still doesn't work.Since main can be void, thus I have kept it void. Do provide me with the solution if you can.Thanks.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    If you want help you should really learn to listen. According to the C standard, the return type for main() cannot be void. If it works for you on your compiler well then lucky you. But around here we stick to standards as much as possible. There's an FAQ for it on this site. Here's a excerpt in particular that talks about it:
    Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

    void foo(void);

    A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int.
    If you understand what you're doing, you're not learning anything.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Abhid
    Hello,
    I thank Ping and invariantloop for their suggestions.I made the 1's to 0's in the if condition,but the code still doesn't work.Since main can be void, thus I have kept it void. Do provide me with the solution if you can.Thanks.
    Like everyone has said, you do not declare main as being void. itsme86 did not post a link to the relevant FAQ, but I will - go read it, and understand that you ought not to be using void main(). Now then, on to your various problems. You need to take a look at what is being written to your file 'Rec'. It will not work as you wish - make some modifications so it spaces things out properly. You need to change your if statements to make it possible for your flag variable to be set to 1. Otherwise, it is always 0, and that is somewhat useless. Also, have a look at your scanf() statements. In the way you are using them, you are inviting a buffer overflow problem, similar to what you could create by using the gets() function. If you must use scanf(), be sure to use a field width specifier, and also check your return value, to be sure the amount of conversions you expect are being done. Or, you could just use fgets() and sscanf() and have a much nicer solution to getting user input.

    That should get you started at least...
    Last edited by kermit; 02-18-2005 at 11:59 PM.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    another point is the use of conio.h which is highly non portable
    and yah, make some changes in your code..
    1) clrscr()==bad.. check the faq to clear the screen
    2)
    Code:
     fprintf(fp,"%s %s\n",p->usr,p->pass);
    this will ensure that all of the things are on a new line and that helps.. the reason is that fscanf that you are using will take in characters till it encounters a white space. now, you can either do what i said, or after the last %s put a space, otherwise the letters will be stored continuously.
    3) also, specifying the type is a good practise.. "rec.txt" is much preferred over just "rec"
    i guess thats all thats required...and yes, main is int and not void. if it works for you, good enuf, but it wont work for all, stick to the standards and avoid bad remarks..just some advice from a fellow indian..chao
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 03:26 AM

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