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; Hi all: I'm trying to write a few lines of code that will take a line of input and replace ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    54

    Finding carriage returns (\c) in a line

    Hi all:

    I'm trying to write a few lines of code that will take a line of input and replace an instance of "\c" with the carriage return character 0xD, and then search a document for that line of input.

    For example, here's a test file (assume saved in Windows format, so each carriage return is \c\l):
    Code:
    Line 1
    Line 2
    Line 3
    Another line 1 but not newline after
    What I want to do is search for "1\c" and have the program return "Line 1"

    My current algorithm is:
    Code:
    char c = 0xD;
    string::size_type pos = contains.find("\\c");
    string carriageReturn(1, c);
    contains.replace(pos, 2, carriageReturn);
    However, this doesn't seem to be working. Any other ideas?

    Thanks.
    -Zack

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The escape character for a carriage return is '\r'.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Prelude
    The escape character for a carriage return is '\r'.
    Hm, are you sure? I'm looking at the table here http://www.lookuptables.com/ and it seems like it's 15, 0xD?

    I just used \c as an example in the input. For example, it takes "1\c" as the input and should look for 1 followed by 0xD in the file.
    -Zack

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    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.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    54
    Thanks guys. I may be a bit dense here, but I'm not sure how I can implement that. Any leads?

    Thanks again.
    -Zack

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Windows line break is a CRLF - control return (\r) line feed (\n). find("\\r\\n") in a loop, and replace it with "\r\n".

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Make sure you're reading-in the file in binary format. If you read it in as text, you will never see CR/LF. It's going to "translate" CR/LF to a NULL-termination for the C-style string format.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    54
    Thanks guys. I'll take a look.
    -Zack

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by DougDbug
    Make sure you're reading-in the file in binary format. If you read it in as text, you will never see CR/LF. It's going to "translate" CR/LF to a NULL-termination for the C-style string format.
    Yup. I have this working for the \t tab character, just not for the new lines.
    -Zack

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Tonto
    Windows line break is a CRLF - control return (\r) line feed (\n). find("\\r\\n") in a loop, and replace it with "\r\n".
    I did this, and it still didn't work .
    -Zack

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Show us what you tried, I guess.

  12. #12
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by twomers
    Show us what you tried, I guess.
    Yeah, I should've done that in the previous post:

    Code:
    else if (contains[contains.size() - 2] == '\\' && contains[contains.size() - 1] == 'c')
    	{
    		string::size_type pos = contains.find("\\c");
    		//cout <<pos <<endl;
    		string carriageReturn(1, c);
    		cout <<contains <<endl;
    		contains.replace(pos, 2, "\r");
    
    		cout <<contains <<endl;
    	}
    -Zack

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    54
    Bump .
    -Zack

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Bumping is against the rules.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the mistake is that you are looking for a sequence of characters like \ and c (should be r?) in the text file, which clearly isn't there. Instead there is an invisible character (well, you see it as a new line beginning), that is represented as an escape sequence '\r', but is really a single character (that has ASCII value of 13).

    Apart from that, the block executes if the last two characters of the string are \ and c. It means you know they are there, why would you use find to find their position again.

Page 1 of 3 123 LastLast
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, 06:41 AM
  2. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 11:22 AM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11: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, 11:30 AM
  5. Carriage Return Line Feeds
    By dattanij in forum C Programming
    Replies: 1
    Last Post: 09-07-2001, 05:41 AM

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