Using C++, how do I read a string of a specific length, from a non-binary file? -


Shows that a line can be read using the getline function. However, I do not want to get the full line; I want to get only a certain number of characters. How can I be done in such a way that character encoding be preserved?

I need a function that does something like this:

  ifstream fileStream; FileStream.open ("file.txt", iOS :: In); As a result & lt; & Lt; GetString (file stream, 10); // Read the first 10 character file. Futel (10); // Go to the next item's result section & lt; & Lt; GetString (file stream, 10); // 10 more characters to read  

I thought about reading a four buffer, but could not this character change the encoding?

I really doubt there is some confusion about the word "character" here. Looking at OP's question, they are using the word "code", which is used for the use of char (as opposed to a rational "character", as the character of multi-byte UTF-8) , And thus the purpose of reading from a text file is interchangeable with the word "byte".

If this is the case, you can read some bytes by using ifstream: read (), example

  ifstream FileStream; FileStream.open ("file.txt", iOS :: In); Four buffers [1024]; Filestream.read (buffer, size (buffer)); Reading in a  char  buffer will not affect the character encoding at all.  

The exact order of the bytes stored on the disk will be copied into the buffer.

However, this is a different story if you are using a multi-byte character set, where each letter is variable-length if the characters are not fixed size, then discs with reading a disc There is no way to read absolutely N characters from This is not a limit of C ++, this is the reality of handling the block device (disk). Blocked devices at the lowest levels of your OS are addressed in terms of blocks, which in turn are made of bytes. so you can always read an exact number of byte from the disk, but you can not read the exact number of logical characters from the disk unless Not every character has a fixed number of bytes. For character sets like UTF-8, where each character has a variable length, you must either read it in the entire file, or read it after reading and reading to read and analyze whether you need to read more . / P>


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -