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. UNICODE problem

UNICODE problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
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.
  • G gabbana

    Hello, i tried to load an unicode file but without luck, but i don't know whats wrong. The first char of the file will not be displayed correctly and then the following data is correct, but at the end there are some more charachters (rectangles) which are not in the file. Here is the code i use: wchar_t *gg; FILE *wfile; wfile = _wfopen(L"ReadMe.txt",L"rb"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,0,SEEK_SET); gg = (wchar_t*)malloc(sizeof(char)*num); fread(gg,sizeof(wchar_t),num,wfile); fclose(wfile); MessageBox(NULL,gg,L"file",0); Thanks for help.

    M Offline
    M Offline
    Matthew Faithfull
    wrote on last edited by
    #2

    You're allocating num * sizeof(char) and then trying to read num * sizeof(wchar_t) . That going to be your first problem :)

    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

    1 Reply Last reply
    0
    • G gabbana

      Hello, i tried to load an unicode file but without luck, but i don't know whats wrong. The first char of the file will not be displayed correctly and then the following data is correct, but at the end there are some more charachters (rectangles) which are not in the file. Here is the code i use: wchar_t *gg; FILE *wfile; wfile = _wfopen(L"ReadMe.txt",L"rb"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,0,SEEK_SET); gg = (wchar_t*)malloc(sizeof(char)*num); fread(gg,sizeof(wchar_t),num,wfile); fclose(wfile); MessageBox(NULL,gg,L"file",0); Thanks for help.

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

      gabbana wrote:

      wfile = _wfopen(L"ReadMe.txt",L"rb");

      For text files, should this be:

      wfile = _wfopen(L"ReadMe.txt", L"r");

      gabbana wrote:

      gg = (wchar_t*)malloc(sizeof(char)*num);

      gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1);
      gg[num] = '\0'; // so that MessageBox() will not fail

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      G 1 Reply Last reply
      0
      • D David Crow

        gabbana wrote:

        wfile = _wfopen(L"ReadMe.txt",L"rb");

        For text files, should this be:

        wfile = _wfopen(L"ReadMe.txt", L"r");

        gabbana wrote:

        gg = (wchar_t*)malloc(sizeof(char)*num);

        gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1);
        gg[num] = '\0'; // so that MessageBox() will not fail

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        G Offline
        G Offline
        gabbana
        wrote on last edited by
        #4

        hmm, the problem still exists: one rectangle at the beginning at many after the file (>16) wchar_t *gg; FILE *wfile; wfile = _wfopen(L"ReadMe.txt",L"r"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,0,SEEK_SET); gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1); fread(gg,sizeof(wchar_t),num,wfile); fclose(wfile); gg[num] = '\0'; // so that MessageBox() will not fail MessageBox(NULL,gg,L"file",0);

        modified on Friday, May 2, 2008 10:16 AM

        D M 2 Replies Last reply
        0
        • G gabbana

          hmm, the problem still exists: one rectangle at the beginning at many after the file (>16) wchar_t *gg; FILE *wfile; wfile = _wfopen(L"ReadMe.txt",L"r"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,0,SEEK_SET); gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1); fread(gg,sizeof(wchar_t),num,wfile); fclose(wfile); gg[num] = '\0'; // so that MessageBox() will not fail MessageBox(NULL,gg,L"file",0);

          modified on Friday, May 2, 2008 10:16 AM

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

          The problem has to do with the FILE structure containing a char type rather than a wchar_t type. Convert from char to wchar_t, like:

          char *gg = (char *) malloc(sizeof(char) * num + 1);
          ...
          USES_CONVERSION;
          LPWSTR lp = A2W(gg);

          MessageBox(NULL, lp, _T("file"), MB_OK);

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          G 2 Replies Last reply
          0
          • D David Crow

            The problem has to do with the FILE structure containing a char type rather than a wchar_t type. Convert from char to wchar_t, like:

            char *gg = (char *) malloc(sizeof(char) * num + 1);
            ...
            USES_CONVERSION;
            LPWSTR lp = A2W(gg);

            MessageBox(NULL, lp, _T("file"), MB_OK);

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            G Offline
            G Offline
            gabbana
            wrote on last edited by
            #6

            Can you give me an example what you exactly mean?

            1 Reply Last reply
            0
            • D David Crow

              The problem has to do with the FILE structure containing a char type rather than a wchar_t type. Convert from char to wchar_t, like:

              char *gg = (char *) malloc(sizeof(char) * num + 1);
              ...
              USES_CONVERSION;
              LPWSTR lp = A2W(gg);

              MessageBox(NULL, lp, _T("file"), MB_OK);

              "Love people and use things, not love things and use people." - Unknown

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              G Offline
              G Offline
              gabbana
              wrote on last edited by
              #7

              okay, but now i only get 3 confusing symbols.

              D 1 Reply Last reply
              0
              • G gabbana

                okay, but now i only get 3 confusing symbols.

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

                What does your modified code look like? Do you have UNICODE and _UNICODE defined?

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                1 Reply Last reply
                0
                • G gabbana

                  hmm, the problem still exists: one rectangle at the beginning at many after the file (>16) wchar_t *gg; FILE *wfile; wfile = _wfopen(L"ReadMe.txt",L"r"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,0,SEEK_SET); gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1); fread(gg,sizeof(wchar_t),num,wfile); fclose(wfile); gg[num] = '\0'; // so that MessageBox() will not fail MessageBox(NULL,gg,L"file",0);

                  modified on Friday, May 2, 2008 10:16 AM

                  M Offline
                  M Offline
                  marcinj
                  wrote on last edited by
                  #9

                  hi, You might want to try: gg = (wchar_t*)malloc(sizeof(wchar_t)*(num+1)); instead of gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1); also, are you sure your file is in UNICODE format where each character is 16 bit wide?

                  G 1 Reply Last reply
                  0
                  • M marcinj

                    hi, You might want to try: gg = (wchar_t*)malloc(sizeof(wchar_t)*(num+1)); instead of gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1); also, are you sure your file is in UNICODE format where each character is 16 bit wide?

                    G Offline
                    G Offline
                    gabbana
                    wrote on last edited by
                    #10

                    In the project options i defined that the linke should use unicode instead of ascii. Now i also tried yours but without an effect. The file was written with notepad and i used the save as option and selected unicode as charset. Okay but now, i am a little bit confusing. i am not sure if i need this for my further learning cycle? Iam actually learning the winapi, but i think for productional use it would be better if i directly use the unicode file stream classes or the windows createfile function to read a file or not ? So i think its not important or i am wrong?

                    M 1 Reply Last reply
                    0
                    • G gabbana

                      In the project options i defined that the linke should use unicode instead of ascii. Now i also tried yours but without an effect. The file was written with notepad and i used the save as option and selected unicode as charset. Okay but now, i am a little bit confusing. i am not sure if i need this for my further learning cycle? Iam actually learning the winapi, but i think for productional use it would be better if i directly use the unicode file stream classes or the windows createfile function to read a file or not ? So i think its not important or i am wrong?

                      M Offline
                      M Offline
                      marcinj
                      wrote on last edited by
                      #11

                      It actually all depends on the project requirements, but I suppose its better to create projects with UNICODE defined and keep your textual data in unicode format. Prefered in my opinion should be UTF-8 which uses less space. If you are loading a text file you should somehow know its encoding type. If you are using Notepad to save text in Unicode format then file contains some extra information bytes about actual encoding type - it is easy to see them in binary file view. Some info on actual notepad format encoding can be found here: http://blogs.msdn.com/michkap/archive/2007/04/22/2239345.aspx[^] There are some other errors in your code that might be the cause of the errory you describe. For example ftell() return length of the file in bytes..., here is a code I got working quite well: wchar_t *gg; FILE *wfile; wfile = _wfopen(L"c:\\ReadMe.txt",L"r"); fseek(wfile,0,SEEK_END); int num = ftell(wfile); fseek(wfile,2,SEEK_SET); int buffSize = sizeof(wchar_t)*(num/2+1); gg = (wchar_t*)malloc(buffSize); memset(gg, 0, buffSize); fread(gg,sizeof(wchar_t),(num-1)/2,wfile); fclose(wfile);

                      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