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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing unicode to file

Writing unicode to file

Scheduled Pinned Locked Moved C / C++ / MFC
ioshelpquestion
11 Posts 4 Posters 0 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.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hi, i have some data in a buffer that i need to write to a file: writing: for(unsigned int i = 4; i<(nReceivedBytes-2); i++) { //_tprintf(_T("%c"), DataBuf.buf[i]); myfile.open ("file.doc", ios::out | ios::app); myfile<<(DataBuf.buf[i]); myfile.close(); } The data is in unicode (2 bytes), the first four bytes show the length of the data package and the last two are zero. The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem? Thank you

    N N D 3 Replies Last reply
    0
    • Y yeah1000

      Hi, i have some data in a buffer that i need to write to a file: writing: for(unsigned int i = 4; i<(nReceivedBytes-2); i++) { //_tprintf(_T("%c"), DataBuf.buf[i]); myfile.open ("file.doc", ios::out | ios::app); myfile<<(DataBuf.buf[i]); myfile.close(); } The data is in unicode (2 bytes), the first four bytes show the length of the data package and the last two are zero. The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem? Thank you

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      yeah1000 wrote:

      myfile.open ("file.doc", ios::out | ios::app);

      What is the type of myfile object - std::ofstream or std::wofstream? When you write unicode data you should use std::wofstream.

      Nuri Ismail

      Y 1 Reply Last reply
      0
      • N Nuri Ismail

        yeah1000 wrote:

        myfile.open ("file.doc", ios::out | ios::app);

        What is the type of myfile object - std::ofstream or std::wofstream? When you write unicode data you should use std::wofstream.

        Nuri Ismail

        Y Offline
        Y Offline
        yeah1000
        wrote on last edited by
        #3

        It was ofstream, i changed it to "wofstream myfile;" but it did't help :( The data arrives over TCP, using WSABUF which i understand is actually of type char. Should i be able to write the data to file two bytes at a time so it could be opened?

        1 Reply Last reply
        0
        • Y yeah1000

          Hi, i have some data in a buffer that i need to write to a file: writing: for(unsigned int i = 4; i<(nReceivedBytes-2); i++) { //_tprintf(_T("%c"), DataBuf.buf[i]); myfile.open ("file.doc", ios::out | ios::app); myfile<<(DataBuf.buf[i]); myfile.close(); } The data is in unicode (2 bytes), the first four bytes show the length of the data package and the last two are zero. The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem? Thank you

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          yeah1000 wrote:

          The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem?

          For application to know that your file is a unicode one, the first two bytes of the file needs to be 0xff and 0xfe. If the first two bytes doesn't indicates any of the encoding codes, it will consider the file and ASCII file. I guess thats what happend in your case. Check http://en.wikipedia.org/wiki/Byte-order_mark[^] for more details.

          nave [OpenedFileFinder] [My Blog]

          Y 1 Reply Last reply
          0
          • N Naveen

            yeah1000 wrote:

            The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem?

            For application to know that your file is a unicode one, the first two bytes of the file needs to be 0xff and 0xfe. If the first two bytes doesn't indicates any of the encoding codes, it will consider the file and ASCII file. I guess thats what happend in your case. Check http://en.wikipedia.org/wiki/Byte-order_mark[^] for more details.

            nave [OpenedFileFinder] [My Blog]

            Y Offline
            Y Offline
            yeah1000
            wrote on last edited by
            #5

            Openoffice sometimes gives me the choice to choose in which format the file is, but when i select unicode then the file still is unreadable. Is there no way to write the file in binary format (as with binarywriter in c# for example) so that it wouldnt matter in which format it is?

            N 1 Reply Last reply
            0
            • Y yeah1000

              Hi, i have some data in a buffer that i need to write to a file: writing: for(unsigned int i = 4; i<(nReceivedBytes-2); i++) { //_tprintf(_T("%c"), DataBuf.buf[i]); myfile.open ("file.doc", ios::out | ios::app); myfile<<(DataBuf.buf[i]); myfile.close(); } The data is in unicode (2 bytes), the first four bytes show the length of the data package and the last two are zero. The file wont open with openoffice and when i open it with notepad++ it displays some characters but most of them are NUL. What could be the problem? Thank you

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Unicode aside, the only data that is going to exist in your file is DataBuf.buf[last_value_of_i]. You should probably be opening and closing the file outside of the for() loop.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              Y 1 Reply Last reply
              0
              • Y yeah1000

                Openoffice sometimes gives me the choice to choose in which format the file is, but when i select unicode then the file still is unreadable. Is there no way to write the file in binary format (as with binarywriter in c# for example) so that it wouldnt matter in which format it is?

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                yeah1000 wrote:

                (as with binarywriter in c# for example)

                What difference does the file have when a string is written in c# using binary mode and in non binary mode?

                nave [OpenedFileFinder] [My Blog]

                1 Reply Last reply
                0
                • D David Crow

                  Unicode aside, the only data that is going to exist in your file is DataBuf.buf[last_value_of_i]. You should probably be opening and closing the file outside of the for() loop.

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  Y Offline
                  Y Offline
                  yeah1000
                  wrote on last edited by
                  #8

                  Filemode is append, it does not help if i place it outside of the for cycle.

                  D 1 Reply Last reply
                  0
                  • Y yeah1000

                    Filemode is append, it does not help if i place it outside of the for cycle.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    yeah1000 wrote:

                    Filemode is append...

                    My bad. I overlooked that piece.

                    yeah1000 wrote:

                    ...it does not help if i place it outside of the for cycle.

                    On the contrary, file I/O is very expensive (compared to reading from or writing to the file). If you are only writing a handful of bytes to the file, however, indeed it will never be noticed. For larger files, it's just plain wrong.

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    Y 1 Reply Last reply
                    0
                    • D David Crow

                      yeah1000 wrote:

                      Filemode is append...

                      My bad. I overlooked that piece.

                      yeah1000 wrote:

                      ...it does not help if i place it outside of the for cycle.

                      On the contrary, file I/O is very expensive (compared to reading from or writing to the file). If you are only writing a handful of bytes to the file, however, indeed it will never be noticed. For larger files, it's just plain wrong.

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      Y Offline
                      Y Offline
                      yeah1000
                      wrote on last edited by
                      #10

                      I am aware of that, i actually open and close the file far outside of the cycle, when i copied part of the code here i changed it to that. However, at the moment the problem is elsewhere. Is it not necessary to define it somehow that i am using tchar? The buffer is in char format, if i write it to file byte by byte, at the end it should not matter that one char is actually 2 bytes in length, assuming that openoffice realizes that it is in unicode format?

                      D 1 Reply Last reply
                      0
                      • Y yeah1000

                        I am aware of that, i actually open and close the file far outside of the cycle, when i copied part of the code here i changed it to that. However, at the moment the problem is elsewhere. Is it not necessary to define it somehow that i am using tchar? The buffer is in char format, if i write it to file byte by byte, at the end it should not matter that one char is actually 2 bytes in length, assuming that openoffice realizes that it is in unicode format?

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        yeah1000 wrote:

                        However, at the moment the problem is elsewhere.

                        True, which is why I prefaced my initial comment as such.

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        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