0

I'm trying to read a binary file with fread() function.
I want to read 2 byte every time (UTF-16 file).
The relative code:

char words[2];
while(fread(&words, sizeof(words), 1, sFile))
//do somthing...

The information from the file is stored only in the first place of the array, and the second stay as zero. Any idea? Thanks

8
  • 3
    "the second stay as zero" --> because the data in the file is zero - every other byte. Commented Nov 9, 2018 at 19:42
  • 1
    How do you write to the file? How do you initialize the data you write? What does fread returns? What does fwrite (which I assume you use to write) return? Please try to create a Minimal, Complete, and Verifiable Example showing both writing and reading. Commented Nov 9, 2018 at 19:44
  • 1
    Try this: char words[2] = {0xaa}; and see if you observe change from 0xaa to some other value. Commented Nov 9, 2018 at 19:45
  • 2
    If the file contains UTF-16 encoded text written in the basic Latin alphabet (or using characters from the ASCII code set), then the UTF-16 values will be U+0020..U+007E, and those are encoded in UTF-16 as one byte containing 0x20..0x7E and the other containing 0x00. Assuming you're using Intel hardware, you have little-endian encoding, so the first byte might be 0x41 (A in ISO 8859-1) and the second would be 0x00. Commented Nov 9, 2018 at 19:47
  • 1
    @vasek Better to try char words[2] = {0xaa, 0x55}; and set both to non-zero. Commented Nov 9, 2018 at 19:47

1 Answer 1

2

Thank to you all, I found the mistake -
In UTF-16, every char is 2 byte, and the "regular" chars are with zero in the 2nd byte.
Again, thank you.

Sign up to request clarification or add additional context in comments.

1 Comment

Though, giving attribution is very much recommended, when writing answer based on what other people wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.