Remove new line (/n) from string

This is a discussion on Remove new line (/n) from string within the C Programming forums, part of the General Programming Boards category; Hi...can someone please help me with the above as its preventing my strstr function from performing correctly....any pointers much appreciated ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Remove new line (/n) from string

    Hi...can someone please help me with the above as its preventing my strstr function from performing correctly....any pointers much appreciated as I am a beginner....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    char *p = strchr( buff, '\n' ) if ( p ) *p = '\0';

    If you're reading input using fgets(), this is all discussed in the FAQ

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Salem
    Code:
    char *p = strchr( buff, '\n' ) if ( p ) *p = '\0';
    Just a minor nit... A missing ';', it should read
    Code:
    char *p = strchr( buff, '\n' ); if ( p ) *p = '\0';
    Salem knows this, it's just a typo...
    Insert obnoxious but pithy remark here

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    And don't forget <string.h>.

    Here's the relevant FAQ entry: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 04:56 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21