EmbLogic's Blog

file i/o (password program)

#include<stdio.h>
#include<stdlib.h>
int main()
{
char *pwd=NULL;
char *buff=NULL;
pwd=(char *)malloc(sizeof(char)*10);
buff=(char *)malloc(sizeof(char)*10);
int i=0,j,ret=0;
int ch;
FILE *fptr=NULL;
printf(“enter the password”);
scanf(“%s”,pwd);
fptr=fopen(“file.txt”,”r”);
if(fptr<0)
{
perror(“fopen”);
return(-1);
}
ch=fgetc(fptr);
for(j=0;j<3;j++)
{
while(ch!=EOF)
{
if(ch!=’\n’)
{
buff[i] = ch;
i++;
}
else
{
ret=strcmp(pwd,buff);
if(ret==0)
{
printf(“login successfully”);
exit(1);
}
else
{
i=0;
}
}
ch=getc(fptr);
}

if(ch==EOF)
{
printf(“try again\n”);
printf(“enter the password”);
scanf(“%s”,pwd);
if(j==2)
{
printf(“\nyou have tried maximum time.\nyour password is blocked\n”);
return (-1);
}
}
}
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>