EmbLogic's Blog

file i/o(changing the existing password in a file )

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *pwd=NULL;
char *pwd1=NULL;
int ch=0,i=0,ret,len;
char *buff;
pwd=malloc(sizeof(char) *10);
pwd1=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 old 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(“\nenter new password::”);
gets(pwd);
printf(“\nconfirm password::”);
gets(pwd1);
ret=strcmp(pwd,pwd1);
while(ret!=0)
{
for(i=0;i<3;i++)
{
printf(“both passwords are not same\n”);
printf(“enter the confirm password again::”);
gets(pwd1);
ret=strcmp(pwd,pwd1);
}
printf(“u have tried maximum times..\n Try again after some time\n”);
break;
}
if(ret==0)
{
len=strlen(pwd1);
ret=fseek(fptr,-(len+1),SEEK_CUR);
ret=fputs(pwd1,fptr);
ret=fputc(‘\n’,fptr);
printf(“\npassword changed..\n”);
}
}
else
{
i=0;
ch=fgetc(fptr);
}
}
}
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>