-2

I have a binary file that I want to read (with fstream) and store in 2 diferent data structures in my program:

  1. the first part, composed of a single line, should be store in a binary tree. Each group of 3 bytes is a node from this tree, and is stored in a struct.

  2. the second part starts after the first's end, and goes to the end of file, and is stored in a single string variable.

this second part I implement this way:

while(!input.eof()) {
  char c;
  input.get(c);
  bitset<8> b(c);
  coded_file = coded_file + b.to_string();
}

but I am having trouble figuring out a way to read the first part. My start point is this:

string line;
getline(input, line);

but after that, how I read the string 3 bytes each time, and at same time moving the pointer in a way that when the code for read the second part start to be executed, it starts in the right point.

Anyone have any tips of how to do that?

jps
  • 15,760
  • 14
  • 59
  • 71
Kleber Mota
  • 8,111
  • 29
  • 86
  • 174
  • `char buffer[3]; ... input.read(buffer, 3); ... input.seekg(0, std::ios_base::end);`??? Assumint by "right point" you mean the end of the file... – fabian May 30 '22 at 19:43
  • @fabian I need read from the `string` that I got with `getline` (this first step cannot go to the end of file) – Kleber Mota May 30 '22 at 19:46
  • @fabian but at the same time, the pointer needs move because in the end of this step, it needs to be in the character after the line, for the second step start from there. – Kleber Mota May 30 '22 at 19:47
  • 1
    @KleberMota `std::getline()` doesn't really make sense with binary file formats. If you have a `std::string` containing the whole file contents, you may use `std::istringstream` and `std::istream::read(), to consumes chunks of 3 bytes. – πάντα ῥεῖ May 30 '22 at 19:48
  • @rturrado ok, that's goot to know. now, how I read the `string` 2 bytes each time (like into a `char[3]` array) and store this in a `struct` (which contains 1 char and 1 int)? – Kleber Mota May 30 '22 at 19:50
  • 2
    Be cautious with `while(!input.eof())`. [It's almost always wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 May 30 '22 at 19:50
  • @πάνταῥεῖ why not? when I write this characters to the file, I am including a `output << endl` to force this to be the first line (isolated). Is this wrong? – Kleber Mota May 30 '22 at 19:52
  • 1
    @KleberMota yes, that's wrong with binary file formats. – πάντα ῥεῖ May 30 '22 at 19:54
  • @πάνταῥεῖ How I should separate this 2 parts If I really need that? – Kleber Mota May 30 '22 at 19:55
  • 1
    @KleberMota you may use some string compatible encoding for each line (e.g. Base64) to represent the plain binary data. Then use some of the plethora of de-/encoding 3rd party implementations, to en-/decode the data for writing/reading as strings. The rest is left for `std::ostringstream::write()` and `std::istringstream::read()` – πάντα ῥεῖ May 30 '22 at 20:03

0 Answers0