ofstream

This is a discussion on ofstream within the C++ Programming forums, part of the General Programming Boards category; Code: cout.put(( int toupper(ch))); What's that int doing there? It looks like you're trying to define a toupper() function or ...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    cout.put((int toupper(ch)));
    What's that int doing there? It looks like you're trying to define a toupper() function or something.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    @Elisya
    I was thinking about to use something like:
    transform (ch.begin(), ch.end(), ch.begin(), toupper);
    but what you're saying sounds easier, thanks.

    @Cpjust
    Yeah i was a bit confused 'bout it , thanks for pointing it out , i'll fix it.
    Last edited by Ducky; 12-16-2007 at 02:43 AM.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    I'm done with it and im glad.
    Im gonna use it to write subtitles and want to offer it to those who are writing them also.
    It seems to work with other extentions than .txt like .srt...
    Thanks again for the help for everybody.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      cout << "Enter file name : ";
      char FileName[99];
      cin  >> FileName;
      ifstream TheFile(FileName);
      ofstream TheCopy;
      TheCopy.open ("YourUppercasedCopy.srt",ios::app);
      string ch;
      
      while (getline(TheFile,ch))
      {      
              ch[0] = (char)toupper(ch[0]);
              TheCopy << ch << endl;
              
      }
      TheCopy.close();
      TheFile.close();
       
    }
    Last edited by Ducky; 12-16-2007 at 03:45 AM.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Using >> to a char buffer is not typically safe (try std::string instead).
    And you're only upping the first letter of each row, in case you wanted to up every letter of every word.
    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.

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    I tried to do it with std::string first but it didnt compile that way i dont know why.
    Code:
    cout << "Enter file name : ";
      string FileName;
      cin  >> FileName;
      ifstream TheFile(FileName);  <-- compile error here
    And upping every word was just a step in between because i didnt know how to up only the first word.
    Last edited by Ducky; 12-16-2007 at 04:58 AM.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    No, of course not... The C++ I/O really could have been done better.
    You can use cin.get or cin.read to fetch X amount of characters from the stream. That way you won't have a buffer overrun if the filename is too long.
    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.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    string FileName;
    cin.get (FileName);

    Doesn't compile either.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Needs a char array, and also specify the size of the array.
    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.

  9. #24
    The larch
    Join Date
    May 2006
    Posts
    3,573
    References

    Code:
    std::string fileName;
    std::getline(std::cin, fileName);
    std::ifstream fin(fileName.c_str());
    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).

  10. #25
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Cool, thanks.
    string fileName;
    getline(cin,fileName);
    ifstream TheFile(fileName.c_str());

    It needed .c_str. Never heard of it.

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    ch[0] = (char)toupper(ch[0]);
    C style casts are discuraged in C++.
    C++ casts make your intent (and effect) more explicit. So I'd probably change that line to:
    Code:
    ch[0] = static_cast<char>( toupper(ch[0]) );
    You're also missing some header files like <string> and <cctype>

  12. #27
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Thanks Cpjust , i changed my code like you said.
    I also liked to add a check if the letter is uppercase after a dot "." .
    Im just not sure how to go about it.
    Can i do the two things in one while() statement or shall i have to use two?
    Can i use only getline() for the two or i need getline() and get()?
    Should i use three files for it or i can do it with two?
    Take File1 , uppercase every line , put it in File2
    Take File2 , uppercase after "." put it in File3
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    
    int main()
    {
      cout << "Enter file name : ";
      string FileName;
      cin  >> FileName;
      ifstream TheFile(FileName.c_str());
      ofstream TheCopy;
      TheCopy.open ("Your_Uppercased_Copy.srt",ios::app);
      
      string str;
      size_t offset;
      
      while (getline(TheFile,str))
      {      
              str[0] = static_cast<char>( toupper(str[0]));
              TheCopy << str << endl; 
      }
      
    
      while((offset = TheFile.find(".")) != -1)
      {
              str =  put.(char( toupper(offset + 1));
               TheCopy << str << endl;          
      }
      TheCopy.close();  
      TheFile.close();
    }
    Last edited by Ducky; 12-19-2007 at 04:36 AM.

  13. #28
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    while((offset = TheFile.find(".")) != -1)
    TheFile is a ifstream which doesn't have the find method. You can find things in a string. Then it would be wiser to compare against std::string::npos which may or may not equal signed value -1.

    However, you should do all the capitalization before you output anything to TheCopy - in the first while loop where you actually have the string that you want to work with.

    By the way, if this is normal text I'd expect a space after '.' unless there is a typographical error. If that is the case you might need to adjust your logic to find the first non-whitespace character after a full stop. You also fail to take into account that '.' may be the last character of the string.
    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).

  14. #29
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Ok, thanks Anon!
    I didnt find out how to use std::string::npos to find a character in a string but i find this method.
    Ok now what i can do is up the first letter of a line and up after any(one) character i want.
    What i cant seem to find is how to uppercase after two characters like ". " (dot and space).
    Code:
    int main()
    {
      cout << "Enter file name : ";
      string   FileName;
      cin >>   FileName;
      ifstream TheFile(FileName.c_str());
      ofstream TheCopy;
      TheCopy.open ("Your_Uppercased_Copy.srt",ios::app);
      
      string str;
      char ch;
      
      while (getline(TheFile,str))
      {      
              str[0] = static_cast<char>( toupper(str[0]));
              
              for(int i = 0; str[i]; i++)
              if (str[i] == '.')
                  str[i+1] = static_cast<char>( toupper(str[i+1]));
              
              TheCopy << str << endl;
      }
      TheFile.close();
      TheCopy.close();
    }
    If i add "(|| str[i] == '. ')" to "if (str[i] == '.')" it's not working. Why?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    What i cant seem to find is how to uppercase after two characters like ". " (dot and space).
    To get this right... what you're saying is that given input like:
    Code:
    hello
    .world
    123 abc
    5678
    You expect output like:
    Code:
    Hello
    .World
    123 Abc
    5678
    Is this correct?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 4 FirstFirst 1234 LastLast
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, 10:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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