Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6318467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:38:28+00:00 2026-05-24T15:38:28+00:00

Hi I wrote a simple c prog to just accept a password while diplaying

  • 0

Hi I wrote a simple c prog to just accept a password while diplaying * to hide the input. But the * for the last character entered is not appearing at the right place.
the code is below

int main(){  
int choice = 0;  
char pass[8];  
FILE *input;  
FILE *output;  
struct termios initial_settings, new_settings;  

if(!isatty(fileno(stdout))){  
    fprintf(stderr,"Not a terminal \n");  
}  
input = fopen("/dev/tty","r");  
output = fopen("/dev/tty","w");  
if(!input || !output){  
fprintf(stderr,"error opening");  
exit(1);  
}  
tcgetattr(fileno(input),&initial_settings);  
new_settings = initial_settings;  
new_settings.c_lflag &= ~ICANON;  
new_settings.c_lflag &= ~ECHO;  
new_settings.c_cc[VMIN] = 1;  
new_settings.c_cc[VTIME] = 0;  
new_settings.c_lflag &= ~ISIG;  
if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) {  
fprintf(stderr,"could not set attributes\n");  
}

int count = 0;  
char ch;  
printf("Please enter the password: ");  
while (count<8){  
ch = fgetc(input);  

if(ch == '\n' || ch == '\r'){  
break;  
}else{  
fputc('*',stdout);  
pass[count] = ch;  
count++;  
}  
tcdrain(fileno(stdout));  
}  


fprintf(output,"you have entered :%s \n",pass);  
tcsetattr(fileno(input),TCSANOW,&initial_settings);  
exit(0);  
}  

The output is as follows:
Please enter the password:* * * * * * *
you have entered :12345678
* pasman@pasman-laptop:~$

Its an 8 character password & Notice that 7 *s appear as expected but the last * is appearing at the end of main.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:38:28+00:00Added an answer on May 24, 2026 at 3:38 pm

    You’re mixing stdio and another stream, output, talking directly to the tty. They have different buffers, and get flushed at different times. You really should just use one of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple program that using Class::ArrayObjects but It did not work as
I wrote a simple web service in C# using SharpDevelop (which I just got
I've wrote simple RMI code to learn how it works. While everything works fine
In this case I wrote a simple prog: int main() { pid_t chpid; chpid=fork();
I wrote simple class in JS witch works, but i had problem when i
I wrote simple servlet but it seems to be working as a single thread
I wrote simple aplication for android. It works great in emulator. But when I
I wrote simple code for deleting all entries from android calendar,but it didn't delete
Using some tutorials I wrote simple widget but it causes error declare customwidget.TestDijit: mixin
While checking LINQ's abilities I've wrote simple QuickSort implementation and was glad that ultimately

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.