Converting to std::basic_istream<TCHAR>

This is a discussion on Converting to std::basic_istream<TCHAR> within the C++ Programming forums, part of the General Programming Boards category; I need to call a function with the following prototype: XmlDocument( std::basic_istream< TCHAR > & stream ) I'm trying to ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    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
    Posts
    7,340
    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
    Location
    Canada
    Posts
    8,048
    As you can see from here, istream is defined as
    Code:
    typedef basic_istream<char, char_traits<char> >
        istream;
    Also, from here you can see that the default second template argument is char_traits<charT>:
    Code:
    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]
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    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, 06:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 06: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

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