Thread: Searching for newline on the end of line text

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Searching for newline on the end of line text

    Hy everyone,

    I've searched high and low for the answer to this but am unable to find a decent answer. The problem is simple, however, I am not a C programmer and its giving me migrane on how to do this.

    What I want to achive is simple:
    - I have a string, within, serveral lines of texts.
    - I need to find out, if the last character in the entire text is a newline.
    - if so, i need to add a string starting without a newline
    - of not, i need to add a string starting with a newline.

    I cannot seem to find a manner in how to verify the last character is newline or not. Remeber, the might be several newlines in the text present, I'm only intrested in the last newline.

    If anyone has any thoughts on this I would really appreciate it!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ftell( ) with SEEK_END will move your file pointer to the last char in the file.

    Now "walk" (move the file pointer), backward, until you reach the first char. If it's a newline, then just write your string.

    If it's not a newline, then write a newline, then write your string.

    An example of using ftell() in this way is here:

    Last line of a data file not wanted to be read in and the while statement

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can use the regexp functions regcomp() and regexec() to filter strings that end in a newline.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >ftell( ) with SEEK_END will move your file pointer to <blah blah blah>
    >You can use the regexp functions regcomp() and regexec() to filter strings that end in a newline.
    What? You guys must be smoking something good to read so far between the lines, because this seems sufficient to me:
    Code:
    if (s[len - 1] == '\n') {
      /* Append a newline first */
    }
    else {
      /* Don't append a newline */
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Prelude View Post
    What? You guys must be smoking something good to read so far between the lines, because this seems sufficient to me
    ROTFLOL
    Quote Originally Posted by Prelude View Post
    Code:
    if (s[len - 1] == '\n') {
      /* Append a newline first */
    }
    else {
      /* Don't append a newline */
    }
    Yup! that one's the best.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You gals must be half asleep to think that someone that new, who asks that basic a question, will know how to find the length of a file.

    ROFL

    Good to see you around again, Prelude. I always enjoy your posts, for real.
    Last edited by Adak; 03-02-2010 at 11:43 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    You gals must be half asleep to think that someone that new, who asks that basic a question, will know how to find the length of a file.
    By using strlen(), of course
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Talking

    Quote Originally Posted by Prelude View Post
    >ftell( ) with SEEK_END will move your file pointer to <blah blah blah>
    >You can use the regexp functions regcomp() and regexec() to filter strings that end in a newline.
    What? You guys must be smoking something good to read so far between the lines, because this seems sufficient to me:
    Code:
    if (s[len - 1] == '\n') {
      /* Append a newline first */
    }
    else {
      /* Don't append a newline */
    }
    Thank you so much for this! I already tried something like this but had the structure and everything completely messed up! :P All other examples where like 30+ lines of code and i could not believe that it needed te be that difficult

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading til end of line
    By bleuz in forum C++ Programming
    Replies: 5
    Last Post: 11-17-2009, 01:54 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. read to end of line problem
    By one1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2003, 04:30 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

Tags for this Thread