0

I'm making a C++ program that, given a Cobol copy and a sequential file, plots the content of the sequential file into a table.

The problem with this is that, for example, "+12345" value in Cobol Comp-3 variables that are structured like this:

 05  COPY-FIELD        PIC S9(05)        COMP-3.

Results in this:

4\

135
24C

If I read the string in C++ and convert it to HEX i get the first byte missing and some differences between what I expected from Cobol, so I can't read the other fields. How could I read the clear bytes from a text file still considering it a string?

user207421
  • 305,947
  • 44
  • 307
  • 483
SidusBrist
  • 45
  • 6
  • There is an article about Cobol Comp-3: [COBOL Comp-3 Packed fields:](http://www.3480-3590-data-conversion.com/article-packed-fields.html). To me, it looks like you should read binary data into a container with `char`s (don't forget the `std::ios::binary` when you open the stream) and then apply some bit-fiddling like described in that doc. However, I must admit it's the first time that I read about "Comp-3". Before today, Cobol was just an ancient chapter of computer science history for me... ;-) – Scheff's Cat Oct 08 '19 at 14:31
  • Concerning `std::ios::binary`: [Binary and text modes](https://en.cppreference.com/w/cpp/io/c#Binary_and_text_modes) – Scheff's Cat Oct 08 '19 at 14:37
  • 3
    A file containing COMP-3 is not a text file and does not contain strings (or lines). – user207421 Oct 08 '19 at 16:06
  • 1
    As the others pointed out: consider this as a binary file and don't read the data as string. Then split the data you find there by using fixed positions into different parts. "text" parts may have a special encoding (if you have national data `PIC N` in there it would be multibyte, some people store UTF-8 "data" in `PIC X`, too - but in most cases you can assume single-byte encoded text). If you don't need to reimplement the wheel you could even use external software to read/convert the data for you like GnuCOBOL)- – Simon Sobisch Oct 08 '19 at 18:37

0 Answers0