Removing end of line marker

This is a discussion on Removing end of line marker within the C Programming forums, part of the General Programming Boards category; I am reading some data from file. One of the data is stored in a character array. The file has ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    11

    Removing end of line marker

    I am reading some data from file. One of the data is stored in a character array. The file has some end of line delimeter. As a result I the character array is also getting the end of line dilimeter . My question is how to remove this end of line delimeter from the character.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You're talking about newlines, '\n'? You can use strchr() to find it's position. Then overwite it with a '\0'.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Create a pointer to a character, search for the last occurance of a newline, and if it's found, change it to a NUL.
    Code:
    char *p = NULL;
    char string[] = "Foo'd up beyond all repair\n";
    if((p=strrchr(string, '\n')) != NULL)
       *p = '\0';
    Last edited by whiteflags; 05-11-2006 at 12:08 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    That should be *p = '\0';
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    I fixed that. Sorry everyone! Heh.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You should also include <string.h>. Otherwise you'll get a warning (if you have them turned on).
    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. Detecting end of LINE in text files
    By kiknewbie in forum C Programming
    Replies: 6
    Last Post: 12-09-2008, 08:34 AM
  2. end of line in text file
    By spveer in forum C Programming
    Replies: 5
    Last Post: 08-18-2005, 12:43 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Removing the last line of a file.
    By skiingwiz in forum C++ Programming
    Replies: 11
    Last Post: 11-11-2003, 10:33 PM
  5. Removing junk from end of string
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 05-20-2002, 01:15 PM

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