EmbLogic's Blog

file i/o(open a file which contains passwords and login by matching the password entered by user.)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *pwd=NULL;
int ch=0,i=0,ret;
char *buff;
pwd=malloc(sizeof(char) *10);
buff=malloc(sizeof(char) *10);
FILE *fptr = NULL;
fptr=fopen(“hello.txt”,”r+”);
if(fptr == NULL)
{
perror(“fopen”);
return -1;
}
else
printf(“file is opened successfully\n”);
printf(“enter the password\n”);
gets(pwd);
ch=getc(fptr);
while(ch!=EOF)
{
if(ch!=’\n’)
{
buff[i]=ch;
ch=getc(fptr);
i++;
}
else
{
ret=strcmp(buff,pwd);
if(ret==0)
{
printf(“login successfully..\n”);
exit(0);
}
else
{
i=0;
ch=fgetc(fptr);
}
}
}
if(ch==EOF)
{
printf(“wrong password..try again\n”);
}
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>