# 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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
