Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I really suspect that there's some confusion here regarding the term "character." Judging from the OP's question, he is using the term "character" to refer to a <code>char</code> (as opposed to a logical "character", like a multi-byte UTF-8 character), and thus for the purpose of reading from a text-file the term "character" is interchangeable with "byte."</p> <p>If that is the case, you can read a certain number of bytes from disk using <code>ifstream::read(),</code> e.g.</p> <pre><code>ifstream fileStream; fileStream.open("file.txt", ios::in); char buffer[1024]; fileStream.read(buffer, sizeof(buffer)); </code></pre> <p>Reading into a <code>char</code> buffer won't affect the character encoding at all. The exact sequence of bytes stored on disk will be copied into the buffer.</p> <p>However, it is a different story if you are using a multi-byte character set where each character is variable-length. If characters are not fixed-size, there's no way to read <em>exactly</em> N characters from disk with a single disk read. This is not a limitation of C++, this is simply the reality of dealing with block devices (disks). At the lowest levels of your OS, block devices are addressed in terms of blocks, which in turn are made up of <em>bytes.</em> So you can always read an exact number of <em>bytes</em> from disk, but you can't read an exact number of logical <em>characters</em> from disk, unless each character is a fixed number of bytes. For character-sets like UTF-8 where each character is variable length, you'll have to either read in the entire file, or else perform speculative reads and parse the read buffer after each read to determine if you need to read more.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload