Thread: Converting to std::basic_istream<TCHAR>

  1. #1
    Registered User
    Join Date
    Jul 2005

    Converting to std::basic_istream<TCHAR>

    I need to call a function with the following prototype:

    XmlDocument( std::basic_istream< TCHAR > & stream )

    I'm trying to open a file and pass the stream in, but I can't figure out how to convert an istream into a basic_istream.

  2. #2
    Registered User
    Join Date
    Jan 2005
    What kind of stream is it? ifstream? An ifstream is a basic_istream<char> and should work if UNICODE is not defined for your project. A wifstream is a basic_istream<wchar_t> and should work if UNICODE is defined for your project. I don't know if this exists but a tifstream would be either an ifstream or a wifstream depending on whether UNICODE was defined or not, and therefore a tifstream would always work.

    If UNICODE is defined, but you have an ifstream and you cannot change it to use a wifstream or tifstream, then you will have to do some extra work.

    If tifstream doesn't exist maybe you could define it yourself. Examples should be readily available.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    As you can see from here, istream is defined as
    typedef basic_istream<char, char_traits<char> >
    Also, from here you can see that the default second template argument is char_traits<charT>:
    namespace std {
      template<class charT, class traits = char_traits<charT> >
      class basic_istream;
    So you need to convert an istream (a.k.a. basic_istream<char, char_traits<char> >) to a basic_istream<TCHAR> (a.k.a. basic_istream<TCHAR, char_traits<TCHAR> >). If you assume that char and TCHAR are the same thing, you can just use a cast -- but that's probably a bad thing to assume. [edit] See Daved's post above. [/edit]

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2005
    You can't use a cast. It either works automatically or it doesn't because UNICODE is defined and the character types are different.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 07:38 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM