Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-16-2003, 07:06 AM
|
#1
|
Member
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344
Rep:
|
Removing newline.
Is this sufficient to remove a newline when getting input from stdin with 'fgets()'?
Code:
if (fgets(password, strcspn(password, "\n"), stdin) == NULL) {
reset_terminal();
perror("fgets: input error, exiting\n");
exit(-1);
}
reset_terminal();
Thank's, Tarts.
Last edited by Tarts; 10-16-2003 at 07:07 AM.
|
|
|
10-16-2003, 12:44 PM
|
#2
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Re: Removing newline.
Quote:
Originally posted by Tarts
Code:
if (fgets(password, strcspn(password, "\n"), stdin) == NULL) {
[...]
|
In this line strcspn() is called before fgets(), because when calling fgets(), the value for the second argument (int size) must be known, so strcspn() will be called to calculate where the newline is, after that fgets() is called with that value as an argument. This is the wrong order, because strcspn() uses the same string (char *password) as fgets() will write into. strcspn() is called first, so the input from stdin is not there yet.
As fgets() will puts exactly one newline at the end of the string it reads, I would first call fgets() to read the string from stdin, and then replace the last character of 'password' with '\0', so the newline will be replaced with the end-of-string marker '\0'. Like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 20
int main()
{
char password[MAXLEN];
printf("\nEnter a pasword: ");
if (fgets(password, MAXLEN - 1, stdin) == NULL) {
fprintf(stderr, "Error fgets(), exiting...\n");
exit(EXIT_FAILURE);
}
password[strlen(password) - 1] = '\0';
printf("\n\n ==> your password is: %s\n\n", password);
return 0;
}
|
|
|
10-16-2003, 12:49 PM
|
#3
|
Member
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344
Original Poster
Rep:
|
Re: Re: Removing newline.
Quote:
Originally posted by Hko
In this line strcspn() is called before fgets(), because when calling fgets(), the value for the second argument (int size) must be known, so strcspn() will be called to calculate where the newline is, after that fgets() is called with that value as an argument. This is the wrong order, because strcspn() uses the same string (char *password) as fgets() will write into. strcspn() is called first, so the input from stdin is not there yet.
As fgets() will puts exactly one newline at the end of the string it reads, I would first call fgets() to read the string from stdin, and then replace the last character of 'password' with '\0', so the newline will be replaced with the end-of-string marker '\0'. Like this:
|
Thank you!
Tarts
|
|
|
All times are GMT -5. The time now is 12:29 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|