Thread: splitting lines

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

    splitting lines

    Hello guys

    What would be the best way to separate strings by '\n' and escape '\r's? To use boost::tokenizer, make own function or just use any already made function?

    Thanks for help

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you are just doing this on like \r\n sequences, then you could just replace every '\n' with an 'r' and the character before it with a '\\'. I don't know what you mean by seperate, but something like strtok modifies the string, so boost::tokenizer could be appropriate.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Assuming you're working with C-style strings (character arrays)...

    You should be able to do almost everything you want with the functions in <cstring>. Most of these functions start with str... strncpy() etc.

    You may need to write some functions on your own, and you may need to insert /n manually in some cases.

    You should not need /r. C-style strings are null-terminated (/n or zero-value character). If you save strings to disk in text mode, the /n will be automatically be converted to carriage return and/or line feed as required by your particular system. If you save in binary mode, you will have to convert the string-termination manually.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the OP wants to turn "foo\r" + "bar" into "foo\\\r\nbar".

    In that case, again assuming you're using C-style character arrays, you could use memmove() to shift all the characters to the right to allow you to add in a '\\'. And strcat() will append one string onto the end of another.
    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.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    What if I just use boost tokenizer separator with ("\n\r")? I heard boost::tokenizer isnt that fast..

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Where did you hear that?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM