Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is "wofstream" useful for?!!

what is "wofstream" useful for?!!

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncement
12 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Superman_

    wofstream is a type definition of the basic_ofstream class with the wchar_t data type. And so it supports wide characters. The 2 statements that you have written are definitely possible.

    «_Superman_»
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

    Polymorphism in C

    J Offline
    J Offline
    Joseph Marzbani
    wrote on last edited by
    #3

    no it's not! it just creates an empty file. I've tried it 10's times.

    _ 1 Reply Last reply
    0
    • J Joseph Marzbani

      no it's not! it just creates an empty file. I've tried it 10's times.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #4

      Have you tried writing the BOM (Byte Order Mark) header in the beginning of the file?

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      J 1 Reply Last reply
      0
      • _ _Superman_

        Have you tried writing the BOM (Byte Order Mark) header in the beginning of the file?

        «_Superman_»
        I love work. It gives me something to do between weekends.

        Microsoft MVP (Visual C++)

        Polymorphism in C

        J Offline
        J Offline
        Joseph Marzbani
        wrote on last edited by
        #5

        yes I have. to be more clear, this damn class writes ASCII characters quiet well. the problem I have is about non-English characters. wofstream of (L"FileName.txt"); of << L"English"; // this works of << L"فارسی"; // this does NOT work of.close();

        A 1 Reply Last reply
        0
        • J Joseph Marzbani

          as I've heard, the class wofstream narrows every thing when it writes to a file. that is it converts every wide character to a single byte one and then writes it to the file. in this way you can't have such a thing as: wofstream out(L"foo.txt"); out << L"unicode string"; so there is one question: what's this WIDE class useful for?!! how you name it WIDE version of "ofstream" while it can't be used with wide characters? and one more thing: why it takes a parameter of type char* as the file name? it's a wide class any way :)

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #6

          It can be used with wide characters, no problem at all. The C++ 98 standard library assumes that all filenames are narrow characters so the first line you've presented won't compile on a standard conforming compiler - however some compilers provide the second form as an extension to the library. What's particularly brain-dead about the constructors are the fact that they take C style strings and not std::basic_strings, but that's another story. The line you've presented works perfectly - it handles wide characters pretty well in my experience. What it stores them in the file doesn't really matter (as far as the standard library is concerned) provided you can read them back in the same form. Cheers, Ash

          J 1 Reply Last reply
          0
          • J Joseph Marzbani

            yes I have. to be more clear, this damn class writes ASCII characters quiet well. the problem I have is about non-English characters. wofstream of (L"FileName.txt"); of << L"English"; // this works of << L"فارسی"; // this does NOT work of.close();

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #7

            Have you tried calling std::basic_fstream::imbue with the locale of the text you want to write? Have a read of Kreft and Langer "Standard C++ IOStreams and Locales" for as many details as you can take and then a few more. Cheers, Ash

            J 1 Reply Last reply
            0
            • A Aescleal

              Have you tried calling std::basic_fstream::imbue with the locale of the text you want to write? Have a read of Kreft and Langer "Standard C++ IOStreams and Locales" for as many details as you can take and then a few more. Cheers, Ash

              J Offline
              J Offline
              Joseph Marzbani
              wrote on last edited by
              #8

              no I have not, and thank you for your clue. I'll read it as soon as return back my home. and one more thing, could you please show me some more resources on these new things (STL::locals). I've no previous experience on that.

              A 1 Reply Last reply
              0
              • A Aescleal

                It can be used with wide characters, no problem at all. The C++ 98 standard library assumes that all filenames are narrow characters so the first line you've presented won't compile on a standard conforming compiler - however some compilers provide the second form as an extension to the library. What's particularly brain-dead about the constructors are the fact that they take C style strings and not std::basic_strings, but that's another story. The line you've presented works perfectly - it handles wide characters pretty well in my experience. What it stores them in the file doesn't really matter (as far as the standard library is concerned) provided you can read them back in the same form. Cheers, Ash

                J Offline
                J Offline
                Joseph Marzbani
                wrote on last edited by
                #9

                no, don't get it wrong. in my case, when I have such a line as of << L"فارسی"; It does not write ANY THING AT ALL into the file. strange, ha?

                L 1 Reply Last reply
                0
                • J Joseph Marzbani

                  no I have not, and thank you for your clue. I'll read it as soon as return back my home. and one more thing, could you please show me some more resources on these new things (STL::locals). I've no previous experience on that.

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #10

                  Sorry, I don't know a good online resource for locales which is why I recommended Kreft and Langer's book. However, Emilio (who posts on here a fair bit) wrote an article about plugging a UTF-8[^] codecvt facet for locales which may give you enough information to get started with part of locales. You can try modifying his code to read and write UTF-16/UCS2/UCS4 depending on your particular needs. Cheers, Ash

                  J 1 Reply Last reply
                  0
                  • A Aescleal

                    Sorry, I don't know a good online resource for locales which is why I recommended Kreft and Langer's book. However, Emilio (who posts on here a fair bit) wrote an article about plugging a UTF-8[^] codecvt facet for locales which may give you enough information to get started with part of locales. You can try modifying his code to read and write UTF-16/UCS2/UCS4 depending on your particular needs. Cheers, Ash

                    J Offline
                    J Offline
                    Joseph Marzbani
                    wrote on last edited by
                    #11

                    thank you so much. :rose: for you :)

                    1 Reply Last reply
                    0
                    • J Joseph Marzbani

                      no, don't get it wrong. in my case, when I have such a line as of << L"فارسی"; It does not write ANY THING AT ALL into the file. strange, ha?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Take a look at this article[^], you are not the first person to struggle with this.

                      It's time for a new signature.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups