Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 514959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:34:43+00:00 2026-05-13T07:34:43+00:00

For some reason my buffer is getting filled with jibberish, and I’m not sure

  • 0

For some reason my buffer is getting filled with jibberish, and I’m not sure why. I even checked my file with a hex editor to verify that my characters are saved in a 2 byte unicode format. I’m not sure what’s wrong.

[on file open]

fseek(_file_pointer, 0, SEEK_END);
this->_length = ftell(this->_file_pointer) / sizeof(chr);

[Main]

//there is a reason for this, I just 
//didn't include the code that tells why
typedef wchar_t chr;
chr *buffer = (chr*)malloc(f->_length*sizeof(chr));
if(buffer == NULL)return;
memset(buffer,0,f->_length*sizeof(chr));
f->Read_Whole_File(buffer);
f->Close();
free(buffer);

[Read_Whole_File]

void Read_Whole_File(chr *buffer)
{
    if(buffer == NULL)
    {
        this->_IsError = true;
        return;
    }
    fseek(this->_file_pointer, 0, SEEK_SET);
    int a = sizeof(buffer[0]);//for debugging purposes  
    fread(buffer, a, _length, this->_file_pointer); 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:34:43+00:00Added an answer on May 13, 2026 at 7:34 am

    Assuming your error handling (that you said you’ve omitted here) is sound, I see two reasons that may be the cause of the problem:

    1. First of all, wchar_t may not necessarily be 2 bytes, its size is implementation defined. For example on Linux it’s most likely 4 bytes.

    2. It may be that the file is UTF-16BE (big-endian), and you are running on a little-endian platform, so the wchar_t values in your buffer have their byte order swapped.

    Or, it may be both. Please update your question with some details about your platform and a few bytes from the sample file in hex (if possible).

    In any case, you should not make any assumptions about sizes of standard C or C++ types when dealing with Unicode files.

    For example, If you want to read UTF16-BE, use C99 uint16_t type (or an equivalent type that’s guaranteed to be 16-bit), and swap byte order of your input depending on your platform endian-ness and file endian-ness. You can detect file endian-ness using a byte order mark if it’s present in the file.

    Alternatively, use a third-part Unicode library, like ICU. It takes care of all platform-specific details and will save you a lot of time debugging in a sizable project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason i cant draw anything past z>1 in openGL. The Z buffer
For some reason, my spec isn't passing. It appears that @categories isn't getting to
For some reason, I am getting a weird overlapping dropdown menu glitch. It works
For some reason I'm getting an error on these lines of code in Python:
I'm having this problem. char buffer[100]; buffer[0] = \n; For some reason, the following
For some reason the size if the window always stays the same size even
For some reason my recvfrom() function for sockets is not blocking on my server
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
for some reason when I try to call CocoaAsyncSocket's onSocket:didReadData:withTag method, it's failing and
For some reason I can't detect I can't set a z index for any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.