urgent!!!i dont know how to compare and read data from a file....

This is a discussion on urgent!!!i dont know how to compare and read data from a file.... within the C Programming forums, part of the General Programming Boards category; I am new c-programming student in TARC...I'm having troubles in my C code in my assignment..hopefully someone can help me ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    urgent!!!i dont know how to compare and read data from a file....

    I am new c-programming student in TARC...I'm having troubles in my C code in my assignment..hopefully someone can help me out...

    My assignment is about write the main program to allow users access into the main menu after they key in their user ID and correct password. After three unsuccessful attempts(i.e wrong password) the program exits.

    the password and userID are stored inside a Password.txt...
    the password must be encrypted. Example of encryption method :

    a becomes z
    b becomes a
    c becomes b
    .
    .
    z becomes y

    e.g. the password "secret" is stored in the file as "rdbqds"
    the real password is obtain by encrypting.

    Now my problem is i dont know how to write a function to allow user to login and a function that check the correct userID with password from the Password.txt file....can someone help me??

    Thanks a lot....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 10
    #define FLUSH while(getchar()!='\n')
    
    FILE *fpRead;
    char userId[N],userPassword[N];
    
    void menu(int *choice);
    void getNewUser(char []);
    void getPassword(char []);
    void encrypt(char ptrEncrypt);
    
    main()
    {
    	int cont,choice;
    	
    
    	if((fpRead=fopen("Password.txt","a+"))==NULL)
    	{
    		printf("\n Error - Cannot open the designated file \n");
    	}else
    	{
    		do{
    			system("cls");
    			menu(&choice);
    			switch(choice)
    			{
    			case 1:
    				{
    					getNewUser(userId);
    					break;
    				}
    			}
    			printf("Do you want to continue?(Y/N)>");
    			cont=getchar();
    			fflush(stdin);
    		}while(cont=='y' || cont=='Y');
    	}
    	return 0;
    }
    
    void menu(int *choice)
    {
    	printf("Menu\n");
    	printf("----\n");
    	printf("1. Register new id\n");
    	printf("2. Login existing user id\n");
    	printf("9. Exit this program\n");
    	printf("Please enter your choice>");
    	scanf("%d",&*choice);
    }
    
    
    void getNewUser(char ptrUser[])
    {
    	int check;
    	int count,i;
    	char temp;
    	
    	do
    	{
    		printf("Please key in your user id>");
    		scanf("%Ns",ptrUser);
    		FLUSH;
    		
    		for(count=0;*(ptrUser+count)!='\0';)
    			count++;
    		if(count>6)
    			printf("Our user id is limited in 1 to 6 character only.\n");
    		else
    		{
    			for(check=0,i=0;*(ptrUser+i)!='\0';i++)
    			{
    				temp=*(ptrUser+i);
    				if(temp>96 && temp<123)
    					check++;
    				else
    				{	
    					printf("Please key in user id in lower case character type.\n");
    					break;
    				}
    			}
    			if(check==count)
    			{
    				for(i=0;i<6;i++)
    					fprintf(fpRead,"%c",ptrUser[i]);
    				fprintf(fpRead,"%\n");
    			}
    		}
    	}while(check!=count);
    	
    	if(count<=6 && count==check)
    		getPassword(userPassword);
    }
    
    
    void getPassword(char ptrPassword[])
    {
    	int count,i;
    	int check;
    	char temp;
    	char ptrEncrypt;
    
    	do
    	{
    		printf("Please key in your password>");
    		scanf("%Ns",ptrPassword);
    		FLUSH;
    		
    		for(count=0;*(ptrPassword+count)!='\0';)
    			count++;
    		if(count>6)
    			printf("Our user password is limited in 1 to 6 character only.\n");
    		else
    		{
    			for(check=0,i=0;*(ptrPassword+i)!='\0';i++)
    			{
    				temp=*(ptrPassword+i);
    				if(temp>96 && temp<123)
    					check++;
    				else
    				{	
    					printf("Please key in user password in lower case character type.\n");
    					break;
    				}
    			}
    			if(check==count)
    			{
    				
    				for(i=0;*(ptrPassword+i)!='\0';i++)
    				{
    					ptrEncrypt=*(ptrPassword+i);
    					encrypt(ptrEncrypt);
    				}
    				fprintf(fpRead,"%\n");
    			}
    		}
    	}while(check!=count);
    }
    
    void encrypt(char ptrEncrypt)
    {		
    	if(ptrEncrypt==97)
    	{
    		ptrEncrypt+=25;
    		fprintf(fpRead,"%c",ptrEncrypt);
    	}
    	else
    	{
    		ptrEncrypt-=1;
    		fprintf(fpRead,"%c",ptrEncrypt);
    	}
    }

    Code tags added by Hammer
    Last edited by calvin; 12-01-2002 at 12:59 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, your problem is never "urgent" to anyone but yourself, and we certainly don't care. Please observe the board guidelines

    >>main()
    Should be
    >int main(void)

    >>scanf("%d",&*choice);
    You mean
    >scanf("%d",choice);
    as choice is already a pointer to an int.

    >>fflush(stdin);
    This is undefined, don't use it. You already have your flush macro anyway, you do know what it's for, right?

    >>for(count=0;*(ptrUser+count)!='\0'; )
    >> count++;
    Why not use strlen() ?

    >>if(temp>96 && temp<123)
    Why not use islower() ?

    >>scanf()
    You should really be using fgets() to get input from the user. It will be much safer for your application

    You have a lot of code to get 2 lines of text from the keyboard, and validate it. Create a seperate function to perform the validate (ie string is all lower case) and get it to return true/false. Use fgets() to get the input. Use standard C functions for doing the simple stuff (as above).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  3. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 03:36 AM
  4. Query file compare data
    By yomama in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2002, 11:06 AM
  5. Help!!!!!!!!
    By Shy_girl_311 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 03:22 PM

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