Binary I/O - Partially Truncated output

I have written a little program to test binary I/O from a simple text.

My program works but when I compare the imported and the exported files, in this last 2 chars are "missing".

Why?

Thanks!

Source file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header1.h"

vector<int> binary_import(vector<int>& b)
{
	string iname;
	cout << "Type filename input: ";
	cin >> iname;

	ifstream ifs{ iname,ios_base::binary };
	if (!ifs) error("Cannot input filename ", iname);

	for (int x; ifs.read(as_bytes(x), sizeof(int));)
	{
		b.push_back(x);
	}

	return b;
}

void binary_export(const vector<int>& b)
{
	string oname;
	cout << "Type filename output: ";
	cin >> oname;

	ofstream ofs{ oname, ios_base::binary };

	for (int x : b)
	{
		ofs.write(as_bytes(x), sizeof(int));
	}
}


Input txt file:
La mia casa si trova in Calabria ed e' esattamente a Cosenza. Questo file sarà analizzato in una modalità
binaria attraverso l'importazione dei dati in quest'ultimo formato.

Output txt file:
La mia casa si trova in Calabria ed e' esattamente a Cosenza. Questo file sarà analizzato in una modalità
binaria attraverso l'importazione dei dati in quest'ultimo format

Notice chars o. are gone in output file.
Last edited on
What I think happens is this:

An int is probably 4 bytes (it usually is) which means your program is reading and writing data in chunks of 4 bytes.

When the only thing left to be read from the file is "to." the read operation will fail because you are trying to read 4 bytes but there are only 3 bytes, so "to." is never added to the vector.
I thought something like that but I was not sure because I am dealing with binary I/O from few time.. In a real situation like that how could this problem be handled? Thanks!
Use char instead of int.
Ok thanks!
Also in real application you want to read data in larger chunks (and probably in another thread):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
std::vector<char> binary_import()
{
    std::string iname;
    std::cout << "Type filename input: ";
    std::cin >> iname;

    std::ifstream ifs {iname, std::ios::binary };
    if (!ifs) error("Cannot input filename ", iname);

    constexpr size_t buf_size = 64;
    char buffer[buf_size];

    std::vector<char> b;
    while (ifs.read(buffer, buf_size)){
        b.insert(b.end(), buffer, buffer + buf_size);
    }
    b.insert(b.end(), buffer, buffer + ifs.gcount());
    return b;
}

void binary_export(const std::vector<char>& b)
{    
    std::string oname;
    std::cout << "Type filename output: ";
    std::cin >> oname;

    std::ofstream ofs{oname, std::ios::binary };
    ofs.write(b.data(), b.size());
}
Note that I use gcount after loop to check if there and how many symbols read before eventual failure. You might use it to adapt your original program (however I have no ide what you would do with missing bytes)
Last edited on
Topic archived. No new replies allowed.