# Bytes

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

![](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACp40jM0IrvzxL-pa9%2F-MACpO3pBO3frOek7P0a%2Fimage.png?alt=media\&token=c439fe4f-308f-41e2-a85e-8f1d22cfb328)

**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](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACp40jM0IrvzxL-pa9%2F-MACr1dgIxvpdTg0phbZ%2Fimage.png?alt=media\&token=10f58328-e71e-4137-b64d-cde3d98ff72b)

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

![Notice instead now the NULL terminator](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACp40jM0IrvzxL-pa9%2F-MACrn0hcXq5U9T9vGCP%2Fimage.png?alt=media\&token=a074e9ad-b3a3-4485-a5a8-72b51c93dc62)

**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](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACp40jM0IrvzxL-pa9%2F-MACt-P2sr-qY105fO7A%2Fimage.png?alt=media\&token=6381c2fe-6c2f-4bb4-a218-e6d52a1e4d55)

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

![](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACp40jM0IrvzxL-pa9%2F-MACtFdMoBb65CK3Pgd_%2Fimage.png?alt=media\&token=39554ccc-e969-4d90-ab46-be3b82745919)

**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.

![](https://4095256017-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M98M7KpH7vBLhacf2Yj%2F-MACu2CGA3hoJ1fpjFeE%2F-MACugn5ddwKvdu9iXKA%2Fimage.png?alt=media\&token=0919244d-fdd7-49ad-a958-fe26f364841d)

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