Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

open dataset ... for input in binary mode

Former Member
0 Kudos

Okay, ten points for the first person who can tell me how to fix this.

I've got a binary file of size 281065 bytes which I want to attach to an e-mail I'm sending from an ABAP programme. Unfortunately, when I do this...

open dataset zfile for input in binary mode.
  do.
    read dataset zfile into ls_xcontent_hex-line.
    if sy-subrc eq 0.
      append ls_xcontent_hex to xcontent_hex.
    else.
      if zcounter eq 1.
        exit.
      else.
        add 1 to zcounter.
      endif.
    endif.
  enddo.
close dataset zfile.

...it manages to ignore the last 55 bytes of the attachment. Now, this being a .pdf file, this is obviously a problem, as it's missing the footer information (which is pretty important for a .pdf). (Aside from this it's all fine though.)

How can I get those last 55 bytes to read? The amount it's reading - 281,010 bytes - is an exact multiple of 255, which is also the line size of the field into which each bit is being read. I just need those last 55 bytes, and it's annoying me

1 ACCEPTED SOLUTION

christian_wohlfahrt
Active Contributor
0 Kudos

Hi Robert!

What is the special reason for your zcounter? You ignore one sy-subrc ne 0, but you don't make an append (in else. add 1...)

Nevertheless, normally line (or file length) is known, that's much easier (as Rich already said, field is big enough for whole line / file).

If this is not possible with big dynamical PDF-files, go for single byte read, step by step. Then you can get every byte, put this internally together and nothing will be lost.

DATA: hex_byte(1) TYPE x, 
            len TYPE i value 1. 

OPEN DATASET zfile FOR INPUT IN BINARY MODE. 


DO. 
  READ DATASET zfile INTO hex_byte MAXIMUM LENGTH 1. 
  IF sy-subrc = 0. 
* append hex_byte to your xcontent_hex
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

CLOSE DATASET file. 

Regards,

Christian

12 REPLIES 12

Former Member
0 Kudos

What is the definition of ls_xcontent_hex-line? May be you should use a string type for this field and try.

Srinivas

former_member188685
Active Contributor
0 Kudos

how you defined <b>line</b> in this ls_xcontent_hex-line.

vijay

0 Kudos

You must make sure that ls_xcontent_hex-line has a length long enough to accept the entire line of the file.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi,

Maybe you should add the keywords <b>MAXIMUM LENGTH</b> and <b>LENGTH</b> as in:


  DO.
    READ DATASET i_srv_file
            INTO e_data
            MAXIMUM LENGTH 1024
            LENGTH l_len.

    IF l_len GT 1024.
*     Line length & exceeds 1024.
      MESSAGE e000 WITH l_len
              RAISING file_read_error.
    ENDIF.

    IF NOT sy-subrc IS INITIAL.
      EXIT.
    ENDIF.

    APPEND e_data.
  ENDDO.

Then you would get feedback if there's a line that's too long.

Regards, Joerg

christian_wohlfahrt
Active Contributor
0 Kudos

Hi Robert!

What is the special reason for your zcounter? You ignore one sy-subrc ne 0, but you don't make an append (in else. add 1...)

Nevertheless, normally line (or file length) is known, that's much easier (as Rich already said, field is big enough for whole line / file).

If this is not possible with big dynamical PDF-files, go for single byte read, step by step. Then you can get every byte, put this internally together and nothing will be lost.

DATA: hex_byte(1) TYPE x, 
            len TYPE i value 1. 

OPEN DATASET zfile FOR INPUT IN BINARY MODE. 


DO. 
  READ DATASET zfile INTO hex_byte MAXIMUM LENGTH 1. 
  IF sy-subrc = 0. 
* append hex_byte to your xcontent_hex
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

CLOSE DATASET file. 

Regards,

Christian

Former Member
0 Kudos

Apologies for that. ls_xcontent_hex-line is a char255. It isn't possible to do line(281065) type c, as the function module I'm using takes a table of type solix.

Any ideas?

Former Member
0 Kudos

People reply so fast on here!!!

The zcounter was an attempt to demonstrate that even forcing it to read an extra line doesn't help.

I like the byte-at-a-time idea - I'll try it. The longest line, by the way, far exceeds 255, it's roughly 15,000 bytes long.

0 Kudos

Yes, I already had doubts about the use of an additional append.

One other option would be: determine total file length in advance, read data in chunks of let's say 100 bytes until you need to read the rest (modulo 100). Just in case byte-by-byte is to slow (and no better solution is proposed).

Former Member
0 Kudos

How about I declare a local field:

data zfield(281065) type x.

Then read it into there, and do it all internally using offsets and lengths?

0 Kudos

You can't go higher than 65535 with type x.

Former Member
0 Kudos

Hi,

Try using the type "LCHR".

Regards,

Suman

0 Kudos

Hi!

Good idea. There is a xstring, which can hold up to 2GB - should be enough. It's recommended to use a binary type for reading binary files, so conversion should be explicit in the program.

Regards,

Christian