Finding carriage returns (\c) in a line

This is a discussion on Finding carriage returns (\c) in a line within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by swoopy And anon has pointed out, it's just one character, so all you need is: Code: else ...

  1. #31
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by swoopy
    And anon has pointed out, it's just one character, so all you need is:
    Code:
    else if (contains[contains.size() - 1] == '\r')
    Or if you prefer hex:
    Code:
    else if (contains[contains.size() - 1] == 0xd)
    Thanks bud.
    -Zack

  2. #32
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    getline is portable. The way it appears to us is perfectly transparent.
    std::endl is also portable. So go ahead and use these two freely.

    However the escape characters are single character tokens. '\n' is not the same thing in Linux and windows. Well... it is. It still represents a new line. However, under windows what one wants to represent a new line is '\r\n', while under Linux it is just '\n'.

    So, in all truth, if you wanted to search for new lines using escape characters, '\n' would be the one. Not '\r'. Because '\n' happens in both systems.

    Regardless, the rules are somewhat simple:

    - getline is the best way to extract a line from a stream. It's portable across systems.
    - std::endl is the best option to input a new line. It's portable across systems.


    And one last thing... '\n' is portable if the file is opened in text mode using a filestream. So, if you write to a ofstream using '\n', it will be automagically converted to '\r\n' under windows and remain '\n' under Linux.

    EDIT: I'm so glad we aren't talking about \c anymore
    Last edited by Mario F.; 07-19-2006 at 05:27 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #33
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > string::size_type pos = contains.find("\\c");
    And of course the same applies here:
    Code:
    		string::size_type pos = contains.find("\r");
    And with the replace. So:
    > contains.replace(pos, 2, "\r");
    Becomes:
    Code:
    		contains.replace(pos, 1, "\r");

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> However, say a customer wanted to search for a carriage return following a word, such as "Line 1\c"
    If you specifically want to allow the user to search for a carriage return following a word, I believe that requires you to read the file in binary using unformatted input, otherwise the carriage return will be converted to a newline. If you don't mind making them search for a generic platform independent newline, then you can use formatted input like getline and forget about the '\r' and '\c' stuff.
    Last edited by Daved; 07-19-2006 at 05:34 PM.

  5. #35
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Daved
    >> However, say a customer wanted to search for a carriage return following a word, such as "Line 1\c"
    If you specifically want to allow the user to search for a carriage return following a word, I believe that requires you to read the file in binary using unformatted input, otherwise the carriage return will be converted to a newline. If you don't mind making them search for a generic platform independent newline, then you can use formatted input like getline and forget about the '\r' and '\c' stuff.
    Yup, I am using binary mode to read the line.
    -Zack

  6. #36
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    However if you search for carriage return you will not find it under linux unless someone explicitly put it there. Search for new line '\n' if what you want is to look for a new line under both systems.

    From what I gathered so far you don't seem to want to look specifically for a carriage return. you want to look generally for a new line. The best way, ensuring portability, is to use an oftsream. Or, at the very least, use getline as anon suggested before.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #37
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Mario F.
    However if you search for carriage return you will not find it under linux unless someone explicitly put it there. Search for new line '\n' if what you want is to look for a new line under both systems.

    From what I gathered so far you don't seem to want to look specifically for a carriage return. you want to look generally for a new line. The best way, ensuring portability, is to use an oftsream. Or, at the very least, use getline as anon suggested before.
    Perhaps I'm not explaining correctly - but I do actually want the actual carriage return (0xD) character. I have a function that does newline characters, now I just need it to do carriage return (and only the carriage return character, nothing more).

    I gave all the examples using newline because I figure that would be the most common case. But I seem to have confused everyone . Sorry about that :P.
    -Zack

  8. #38
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    No worries mate. you got it already. have fun
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  2. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 10:10 PM
  4. Finding the line and file in which a function was called
    By roktsyntst in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2003, 10:30 AM
  5. Carriage Return Line Feeds
    By dattanij in forum C Programming
    Replies: 1
    Last Post: 09-07-2001, 04:41 AM

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