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; On second thought, it's a bit unclear what you are trying to achieve. Do you want to get only the ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    On second thought, it's a bit unclear what you are trying to achieve. Do you want to get only the line that ends with a particular character/substring?

    In this case you could use a combination of
    getline() to get a line from a text file,
    rfind() to find the last occurence of a character/substring,
    and size() (for example) to test if the substring is at the very end of the string.
    Last edited by anon; 07-19-2006 at 04:07 PM.

  2. #17
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Wraithan
    Bumping is against the rules.
    Sorry .
    -Zack

  3. #18
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by anon
    On second thought, it a bit unclear what you are trying to achieve. Do you want to get only the line that ends with a particular character/substring?

    In this case you could use a combination of
    getline() to get a line from a text file,
    rfind() to find the last occurence of a character/substring,
    and size() (for example) to test if the substring is at the very end of the string.

    Code:
    getline(FileIn, line);
    pos = line.rfind(substring);
    if (pos == npos && substring.size()+pos == line.size()
    //you got it

    Please forgive my awful explanation. So here it is, I'll attempt again:

    Essentially, I'm looking for occurrences of a particular string within a line. For example, imagine the following file:
    Code:
    Line 1
    Line 2
    Line 3
    Assuming this file is saved in Windows format, if I searched for "Line 1\c" then I should return the line in which the string I searched for is found - namely, "Line 1" should be returned.

    Does this make more sense?

    Thanks.
    -Zack

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    But what on earth is \c?
    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. #20
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Mario F.
    But what on earth is \c?
    We're using it as the carriage return character. As I understand now, the C++ representation of character 13 is \r. In our program, the user would type "\c"

    Thanks.
    -Zack

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by JizJizJiz
    We're using it as the carriage return character. As I understand now, the C++ representation of character 13 is \r. In our program, the user would type "\c"
    So the input file you would be searching would look like this?
    Code:
    Line 1\cLine 2\cLine 3
    As a txt file everything would be on one line? And now you want to break it to actual lines? (- Replace all "\\c"-s with "\r\n")
    Or search for "Line 1"? (s.find("Line 1")
    Or search for "Line 1\c" (s.find("Line 1\\c")?

    Why do you make the user type "\c" in the first place instead of just letting them hit Enter?
    Last edited by anon; 07-19-2006 at 04:44 PM.

  7. #22
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by anon
    So the input file you would be searching would look like this?
    Code:
    Line 1\cLine 2\cLine 3
    As a txt file everything would be on one line? And now you want to break it to actual lines? Or search for "Line 1"? Or search for "Line 1\c"?

    Why do you make the user type "\c" in the first place instead of just letting them hit Enter?
    The text file itself would have the actual new lines. So like, it would look just like what I posted above. However, say a customer wanted to search for a carriage return following a word, such as "Line 1\c"

    The Windows binary representation for Line 1 should be something like Line 1\r\l. Since I'm replacing \c with \r in the actual search.
    -Zack

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    The problem with \c is that the user would actually have to input "\\c".
    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.

  9. #24
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Mario F.
    The problem with \c is that the user would actually have to input "\\c".
    Well, I parse the user's input. Say the user writes: "Line 1\c" - what I actually pass to the function that does the file searching is: "Line 1\r"

    I think this works, right?
    -Zack

  10. #25
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Lol.. look Jiz... you are either confusing yourself to death, or us

    Forget \c ok... Lets just pretend \c doesn't exist. Better, let's not pretend. \c doesn't exist. ok?

    Now...

    What you want is to search for a line that ends with a given string. And anon already told you how to do that. With getline and such... look at that post of his.

    Now... you also want to return the line number at which that happens. For that you use anon code and wrap it inside a loop. You increment a counter inside the loop. Everytime the string is found, you output the counter.
    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.

  11. #26
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok! Why you have to replace '\r' with '\c' just beats me, but OK.

    Are you sure you understand that '\r' as a carriage return symbol is one character, as opposed to "\\r" which is two characters (backslash+r). If you understand that, there should be no problems...

  12. #27
    Registered User
    Join Date
    May 2005
    Posts
    54
    I think I'm confusing myself to death, and in the process, everyone else too. I'm sorry :P. I kinda feel like an idiot now.

    So essentially, I'm writing a program that works on both Windows and Unix. It needs to search through a file and return lines that contain a certain string. I'm beginning to question myself why I should use \r. I'm going to try to figure things out and get back to this thread.
    -Zack

  13. #28
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not wrong, getline() get's a line from a text file, no matter what the OS is? So this should be the way to go? It ignores line delimiters, right, so you wouldn't have to worry about them at all. And if you want to put a newline in a text file, you'd just use endl or '\n' ?

  14. #29
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by anon
    If I'm not wrong, getline() get's a line from a text file, no matter what the OS is? So this should be the way to go? It ignores line delimiters, right, so you wouldn't have to worry about them at all. And if you want to put a newline in a text file, you'd just use endl or '\n' ?
    Ah, if getline() ignores line delimiters, that may be why my original method isn't working...is there something else I should try using?
    -Zack

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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)

Page 2 of 3 FirstFirst 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, 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