LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-16-2003, 07:06 AM   #1
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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.
 
Old 10-16-2003, 12:44 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}
 
Old 10-16-2003, 12:49 PM   #3
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newline problem harrylmh Programming 4 11-21-2005 05:04 AM
php + newline??? blizunt7 Programming 1 06-20-2005 12:44 AM
Newline in xmessage? scottjwoodford Linux - General 2 06-09-2005 04:17 PM
problems with newline in awk test Programming 6 05-02-2004 03:33 AM
What is a newline? raptorsoft2000 Linux - Newbie 6 08-04-2003 11:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration