Thread: ofstream

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes, exactly.
    And i wanna do it by reading a string.
    I can do it with a char ch; , but i wanna be able to do it with a string like in the example.
    I wanna choose two characters like ". ." two dots for example and uppercase the letter that comes right after.

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    And i wanna do it by reading a string.
    Yes, it makes sense to read each line into a string.

    I wanna choose two characters like ". ." two dots for example and uppercase the letter that comes right after.
    More generally, you want to find the first alphabetic character in the line, and make it uppercase. isalpha() from <cctype> can help you, but need will need to loop character by chararacter within the string, and break out of the loop as soon as you have used toupper() to change the character found.

    EDIT:
    Ah, but the find_first_of member function of std::string can help you do away with the character by character loop and the use of isalpha(), though at the cost of having to list the entire alphabet in both uppercase and lowercase.
    Last edited by laserlight; 12-25-2007 at 08:52 AM.
    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

  3. #33
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks for your answer but :
    "More generally, you want to find the first alphabetic character in the line"
    No, the proggie that i posted does this already.


    The problem with "find_first_of" it checks only one character not a sequence of characters:

    From http://www.cplusplus.com/reference/s..._first_of.html
    "Notice that for a match to happen it is enough that one of the characters matches in the string (any of them). To search for an entire sequence of characters use find instead."

    And "find" doesnt work with ifstream. Now you see where is my problem.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, the proggie that i posted does this already.
    It does? If it does, then your problem has been solved. I think it does not.

    The problem with "find_first_of" it checks only one character not a sequence of characters:
    What sequence of characters do you want to find?

    And "find" doesnt work with ifstream. Now you see where is my problem.
    Recall that you have read the line into a string.

    EDIT:
    I have this feeling that you changed your requirements midway through this thread. You need to get your requirements straight if you want people to help you coherently.

    I asked you if given this input:
    Code:
    hello
    .world
    123 abc
    5678
    You wanted this output:
    Code:
    Hello
    .World
    123 Abc
    5678
    You agreed. But you also wrote that: "I wanna choose two characters like ". ." two dots for example and uppercase the letter that comes right after."

    So, you have a choice. Given this input:
    Code:
    hello
    .world
    123 abc
    5678
    ..def
    ..ghi
    Do you want:
    Code:
    hello
    .world
    123 abc
    5678
    ..Def
    ..ghi
    or
    Code:
    hello
    .world
    123 abc
    5678
    ..Def
    ..Ghi
    or
    Code:
    Hello
    .World
    123 Abc
    5678
    ..Def
    ..Ghi
    ?
    Last edited by laserlight; 12-25-2007 at 09:51 AM.
    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

  5. #35
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    ....
    Last edited by Ducky; 12-25-2007 at 12:30 PM.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Im gonna put it like this : i wanna find in a phrase two characters consecutive
    ( one beside the other , one right after the other , like for example i wanna find "ra":
    you find "ra" in the word "phrase" and uppercase the letter right after "ra" which gives you :
    phraSe. ) Do you get it now?
    Ah. In this case, the find() member function of std::string is what you need. You have to learn how to handle the lack of the search string, but as mentioned you just compare the return value of find() with std::string::npos.

    And i said yes before when you showed me your example because i want my program to do different things
    and for the other things you asked it can already do it.
    That's good.
    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

  7. #37
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    So you're saying that find() is working with ifstream, because just before you, Anon said it didnt
    and if you check out the ifstream reference , there is no find() in it.
    http://www.cplusplus.com/reference/iostream/ifstream/
    Thanks!

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So you're saying that find() is working with ifstream, because just before you, Anon said it didnt
    and if you check out the ifstream reference , there is no find() in it.
    I'm not. I'm saying find() works with std::string, i.e., you need to read from the ifstream into a string. You are already doing this with your use of std::getline().
    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

  9. #39
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool. Thanks for clearing this up to me :-)
    Last edited by Ducky; 12-25-2007 at 01:09 PM.

  10. #40
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok i checked out find() but i must do something wrong
    Code:
    found=str.find('.');
             if (found!=string::npos)
             str = static_cast<char>( toupper((int(found))+1));
    TheCopy << str << endl;
    Maybe its the (int(found))+1).
    How shall i tell it to the program that i want to use toupper() on the character after the found string?

  11. #41
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    str = static_cast<char>( toupper((int(found))+1));
    Do you have a concrete idea of why this would do what you want, or did you just write random code and hope it works?
    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

  12. #42
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Im afraid its the latter.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the +1 shows you have an algorithm in mind, so +1 to you. However, what you're missing is that you want to make an character uppercase, but you never actually access the character (str[found+1]), and then you attempt to assign to a string (str), not a character. Of course, you also need to handle the case where the '.' is the last character, in which case there is no character to turn uppercase.
    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

  14. #44
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you very much Laserlight , you're very nice and you teach me a lot!
    I found it at last (with your help) and it's working.
    Code:
    found=str.find(". ");
             if (found!=string::npos)
             str[found+2] = static_cast<char>( toupper(str[found+2]));
    As to the problem with the dot at the end of the line, actually what i want is to capitalize the
    next line's first letter if the previous line ends with a dot.
    That would look something like this: found=str.find(".\n");
    and that works great with .get() but it doesnt wanna work with getline().
    Im wondering why...

  15. #45
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because 1) getline() swallows the newline and probably 2) you read only one line at a time.
    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. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 11:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 10:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM