Thread: ofstream

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    ofstream

    Hi again!

    So here's what im trying to do this time:

    1.Open a file.
    2.Capitalize the first letter of every line.
    3.Put it in another file.

    I got stuck with the third part. It capitalize every word on the console but not in the
    created file.



    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
     
      ifstream TheFile("f2.txt");
      ofstream TheCopy;
      TheCopy.open ("YourCopy.txt",ios::app);
      string ch;
      while (getline(TheFile,ch))
      {      
              cout.put((int toupper(ch)));                         
              else cout.put(ch);
              TheCopy << ch;
            
      }
      TheCopy.close();
      TheFile.close();   
      cout << endl;
      system("pause");   
    }
    I hope i dont abuse your patience. Tx.
    Last edited by Ducky; 12-16-2007 at 02:06 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why don't you open TheCopy only once before you start looping?

    Also, don't hope to read "*.doc" files this way. Just create a simple text file with Notepad or similar and save to a plain text file. A Word document has a lot more in it than just the plain text and you'll probably just make the resulting file unreadable.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    while (TheFile.get(ch))
    Reading a line and then upping each character using an array is more efficient, but that's up to you if you want to add/do.

    Code:
          if (pre == ' ' || pre == '\n') 
    
            cout.put(char(toupper(ch)));                              
            else cout.put(ch);
    Indent this and the following code properly first. The line after if should preferably be indented by a tab so we know that it's the only line line associated with the if. And the rest of the code afterwards should be on the same level as the while. Indentation is to help us keep track of where the blocks lie, not create confusion, which is exactly what you're doing.

    Also, from what I can see, it only ups the character IF it's a space ' ' or a new line '\n'; otherwise it just prints out a wrong character.

    And opening and closing a file directly after writing a char is a huge waste of time. It means it has to do the work to open the file, then write a character and when you close, it has to flush the buffer. As anon suggests, open the out file before the loop and close it after.
    It also so happens that you try to close the output file twice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    your program only makes " " and "\n" upper. at the beginning of your loop you should write it
    with the not operator
    Code:
          if (! pre == ' ' || pre == '\n')

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok, you have modified the original code, but you could have also notified us of it and told us what is not right now.

    Code:
      char fileName[80];
      char buffer;
    These are both unused and should be removed.

    Code:
     
      ifstream TheFile("f2.txt");
      ofstream TheCopy;
      TheCopy.open ("YourCopy.txt",ios::app);
      char ch, pre = '\0';
    ...
        if (pre == ' ' || pre == '\n')
    Does it give you an idea why the first word is not capitalized - unless the file begins with a space?

    Code:
      while (TheFile.get(ch))
      {
     
          if (pre == ' ' || pre == '\n') 
    
              cout.put(char(toupper(ch)));                              
              else cout.put(ch);
            
       TheCopy << ch;
       pre = ch;
            
      }
    Do you have any idea why you see correct output on the screen (apart from the first word) but not in the output file?

    Hint, toupper does not modify the argument. It returns the upper-case character.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Aron, Elisia and Unlockitall!

    I intented and fixed the code. Its the same, its working on the console output but it writes
    to the new file without upping the first letters.

    @Unlockitall I think its good as it is because its working.The idea behind this
    if ( pre == ' ' || pre == '\n') is that after a white space or a new line character there must
    be a word.

    @Elisya "Reading a line and then upping each character using an array is more efficient, but that's up to you if you want to add/do."

    I only liked to uppercase every word's fist letter not every letter.

    @Anon
    - yes i was wondering why the first word wasnt capitalized Tx But i still dont get it. "Pre" is initialized to "\0". Why would it
    be a problem, i dont see it.
    - so if toupper() does not modify the argument, i guess another function that does it is not an option
    so maybe i should find a workaround.
    Last edited by Ducky; 12-15-2007 at 12:08 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    @Elisya "Reading a line and then upping each character using an array is more efficient, but that's up to you if you want to add/do."
    Sure, maybe I worded it a little wrong. You currently just read on a char-by-char basis, but you can also do the same code with reading a whole line and an array. The logic will be the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    @Anon - yes i was wondering why the first word wasnt capitalized Tx
    - so if toupper() does not modify the argument, i guess another function that does it is not an option
    so maybe i should find a workaround.
    For the first problem perhaps try initializing pre to either ' ' or '\n', so the if condition also picks up the first word (as if there was a space before).

    To change a letter to uppercase, perhaps try:
    Code:
    ch = toupper(ch);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @Anon
    Cool, im so happy the first problem works great.
    The second unfortunately not, i added it to the code of my first post, i hope i did it well.
    Actually it capitalizes every letter so thats already a progress though.

    @Elisya
    Thanks for the advice , i dont know how to do it that way but im gonna look for it.
    Last edited by Ducky; 12-15-2007 at 12:31 PM.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about putting this line in the if block (and then printing both to screen and file unconditionally)?

    I wonder how you managed to come up with the program if you can't figure out such simple things...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    lol You got me!

    Maybe i should go to a Beginner's board instead.
    Thanks

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @Elisya
    I dont think that fstream supports arrays.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it does. Operators << and >> are pretty poor (and dangerous if misused), but there is also get and write from which you can read into an array. Then there's basic_istream.getline which works with array (but not std::string).
    If you need to read into an std::string (recommended), then you can use getline to read into an std::string from your file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok i figured out how to use getline with std::string
    Code:
    string ch ;
      while (getline(TheFile,ch))
      {
          
              cout.put((int toupper(ch)));                            
              else cout.put(ch);
              
        TheCopy << ch;
      }
    But now it poses the problem on how to use toupper() only on the first character of the string.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    ch[0] = (char)toupper(ch[0]);
    std::string overloads operator [], so you can use it just like in a normal C-style string to access characters in the string (and assign them!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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