> For the complete documentation index, see [llms.txt](https://ref.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ref.gitbook.io/notes/encoding/bytes.md).

# Bytes

Given this Windows text file open in hexedit. I'm using Visual Studio.

![](/files/-MACpO3pBO3frOek7P0a)

**Scenario 1:**

Open using `std::ifstream jconfig("jconfig.cfg");`

Using `ls -l` it has a file size of 17 bytes.&#x20;

On code snippet:

```cpp
begin = jconfig.tellg();
jconfig.seekg(0, std::ios::end);
end = jconfig.tellg();
int n = end - begin; // n is 17
```

But when loading content into array, the array has size of 15 bytes

```cpp

std::vector<unsigned char> cfg(std::istreambuf_iterator<char>{jconfig}, {});
n = cfg.size(); // n is 15
```

Then examining cfg buffer shows:

![So two 0D are lost](/files/-MACr1dgIxvpdTg0phbZ)

And then using `std::getline` and checking one line, shows:

![Notice instead now the NULL terminator](/files/-MACrn0hcXq5U9T9vGCP)

**Scenario 2:**

Open file is binary.

This snippet gives 17:

```cpp
begin = jconfig.tellg();
jconfig.seekg(0, std::ios::end);
end = jconfig.tellg();
int n = end - begin; // n is 17
```

Then this snippet shows exactly the content:

```cpp

std::vector<unsigned char> cfg(std::istreambuf_iterator<char>{jconfig}, {});
n = cfg.size(); // n is 15
buf = cfg.data();
```

Examining `buf` shows:

![Content is intact](/files/-MACt-P2sr-qY105fO7A)

And then using `std::getline` examining one line of read gives:

![](/files/-MACtFdMoBb65CK3Pgd_)

**Full code below:**

```cpp
int n;
std::ifstream jconfig("jconfig.cfg", std::ios::binary);
//std::ifstream jconfig("jconfig.cfg");

std::streampos begin, end;

if (jconfig.is_open()) {
    std::string line;
    std::cout << "go"; 

    begin = jconfig.tellg();
    jconfig.seekg(0, std::ios::end);
    end = jconfig.tellg();
    n = end - begin;
    const char* buf;
    unsigned char* buffer;

    jconfig.seekg(0, std::ios::beg);

    std::vector<unsigned char> cfg(std::istreambuf_iterator<char>{jconfig}, {});
    n = cfg.size();
    buffer = cfg.data();

    jconfig.seekg(0, std::ios::beg);

    while (std::getline(jconfig, line)) {
        std::cout << line; 
        buf = line.c_str();
    }
}
```

Here is another Unix file, contents in hexedit. Doing `ls -l` shows it has 47 bytes.

![](/files/-MACugn5ddwKvdu9iXKA)

```cpp
begin = jconfig.tellg();
jconfig.seekg(0, std::ios::end);
end = jconfig.tellg();
n = end - begin; // 47
const char* buf;
unsigned char* buffer;
```

... all read seems 47 fine. Be careful with notepad?
