> 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?


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ref.gitbook.io/notes/encoding/bytes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
