Wednesday, June 24, 2015

Comparing Username Stored in File in C

I got a request to read the username from file and compare the same .
Here is the code for the same :

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
char user_name[128];
char line[128];
char str[128];
char *ptr;
int i=0;
FILE *fp;

//Saving username in file
printf("Enter the username to write in a file:");
ch=getch();
while(ch!=13)
{
if(ch==8)
{
printf("\b");
putch(NULL);
printf("\b");
user_name[i]='\0';
--i;
}
else
{
printf("%c",ch);
user_name[i++]=ch;
}
ch=getch();
}
user_name[i]='\0';
ptr=user_name;
fp=fopen("d:\\user.txt","w+");
while(*ptr!='\0')
{
fputc(*ptr,fp);
ptr++;
}
printf("\nUsername successfully saved in the file");

//Reading username from file and saving to string .
fseek( fp, 0, SEEK_SET );
while(fgets(line,sizeof(line),fp)!=NULL)
   {
   char *nwln=strchr(line,'\n');
   if(nwln!=NULL)
   *nwln='\0';
   }
printf("\nUsername in file is :%s",line);
fp=NULL;

//take username from user to compare.
printf("\nEnter username to confirm:");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = '\0';
if(!strcmp(line,str))
{
    printf("\nyou are right");
}
else
{
    printf("\nyou are wrong");
}

getch();
}


4 comments:

  1. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog. We are also providing the best services click on below links to visit our website.
    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

    ReplyDelete
  2. This is an informative example of handling file operations and comparing usernames in C programming. It helps learners understand how file handling, string comparison, and conditional logic work together in practical applications. Tutorials like this are valuable for improving coding skills and understanding real-world programming concepts. Thanks for sharing this useful resource!
    ms azure devops online training

    ReplyDelete
  3. This is an interesting example of file handling in C programming. Comparing a username stored in a file helps learners understand file operations, string handling, and comparison logic. Practical examples like this make it easier to connect programming concepts with real-world applications. Thanks for sharing!
    mlops and aiops online training

    ReplyDelete

Feel free to comment......