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: 

How to read the application server file which is in binary mode

Former Member
0 Kudos

Hi All,

Could you please help me, how to read the contents of an application server file which is in binary mode.

Thanks for your help in advance.

11 REPLIES 11

Former Member
0 Kudos

Suresh,

You can open the files for input / output in BINARY mode.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE

OPEN DATASET FNAME FOR INPUT IN BINARY MODE

regards,

Ravi

Note : Please mark the helpful answers

0 Kudos

how to use read dataset statement

Former Member
0 Kudos

Hai suresh,

To read data from a file on the application server, use the READ DATASET statement:

Syntax

READ DATASET <dsn> INTO <f> [LENGTH <len>].

regards,

praba.

0 Kudos

hi praba, i need syntax for read dataset in the case of binary file reading, can you help me

0 Kudos

Hi Suresh,

The way in which the file was opened during the statement OPEN DATASET determines how records are read from the dataset.

If the file was opened in text mode (addition IN TEXT MODE or IN LEGACY TEXT MODE beim OPEN DATASET), the system assumes that the file is structured line by line. Accordingly, read commands always refer to one line. Line separators are viewed as structuring units and therefore automatically filtered.

If the file was opened in binary mode (addition IN BINARY MODE or IN LEGACY BINARY MODE with OPEN DATASET), the system expects an unstructured sequence of bytes. Length specifications accordingly refer to the number of bytes.

The number of characters or bytes read by calling READ DATASET depends, on the one hand, on the opening mode of the file, and, on the other hand, on the type of target field f.

So, the read has to be used in the same way for both binary and text modes. Its the opening of the dataset that makes the difference.

Hope this will help.

Thanks,

Rashmi.

0 Kudos

Hi Rashmi , if my file is of structure

File Name K01 MIFILE A 10

Member Name MIMBR A 10

Creation Date Yymmdd K02 MICRTDT P 6 0

Creation Time Hhmmss K03 MICRTTM P 6 0

Nbr of Current Rcds MINCRCDS P 10 0

then can you please tell me the syntax to read it

0 Kudos

Hi,

Declare the variable to number of characters that you have in one line.

As per teh structure that you have given i think one line for you will be around 42 characters.

So,

Open dataset <dataset name> for input in binary mode.

read dataset <dataset name> into <var> length 42.

Here 42 is the length in bytes. for example char is 1 byte, int will be 2 bytes. So based on this we have to see the length.

Thanks,

Rashmi.

0 Kudos

Hi Rashmi,

Thanks for helpful answer, cab you pls send your yahoo mail id.

Former Member
0 Kudos

hai suresh,

here is an example program for it,

while working in binary mode, you can use the LENGTH addition to find out the length of the data transferred to <f>. The system sets the value of the variable <len> to this length.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',

TEXT2(5),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.

TRANSFER TEXT1 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

DO.

READ DATASET FNAME INTO TEXT2 LENGTH LENG.

WRITE: / SY-SUBRC, TEXT2, LENG.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

hope this will be helpful for u.

regards,

praba.

Former Member
0 Kudos

Hi

To read data from a file on the application server, use the READ DATASET statement:

Syntax

READ DATASET <dsn> INTO <f> [LENGTH <len>].

This statement reads data from the file <dsn> into the variable <f>. In order to determine into which variable you should read data from a file, you need to know the structure of the file.

You can specify the transfer mode in the OPEN DATASET statement. If you have not already opened the file for reading, the system tries to open it either in binary mode, or using the additions from the last OPEN DATASET statement. However, it is good practice only to open files using the OPEN DATASET statement. For further information about the OPEN DATASET statement and the naming conventions for files, refer to Opening a File.

If the system was able to read data successfully, SY-SUBRC is set to 0. When the end of the file is reached, SY-SUBRC is set to 4. If the file could not be opened, SY-SUBRC is set to 8.

If you are working in binary mode, you can use the LENGTH addition to find out the length of the data transferred to <f>. The system sets the value of the variable <len> to this length.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',

TEXT2(5),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.

TRANSFER TEXT1 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

DO.

READ DATASET FNAME INTO TEXT2 LENGTH LENG.

WRITE: / SY-SUBRC, TEXT2, LENG.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

I hope this will help you to solve your problem.

Thanks

Mrutyunjaya Tripathy

vinod_gunaware2
Active Contributor
0 Kudos
  • sy-subrc = 0 Record read from file

  • sy-subrc = 4 End of file reached

data: w_dataset1(27) value '/var/textfile.txt',

w_dataset2(27) value '/var/outfile.txt'.

data:begin of itab1 occurs 0, "Text file format

MIFILE A 10

MIMBR A 10

MICRTDT P 6 0

MICRTTM P 6 0

MINCRCDS P 10 0

end of itab1.

*Uploading of text file from Application server.

open dataset w_dataset1 for input in text mode.

do.

if sy-subrc <> 0.

exit.

endif.

read dataset w_dataset1 into itab1.

append itab1.

clear itab1.

enddo.

close dataset w_dataset1.

U have to handle all time and date relatlated changes after reading file.

Hope it will be useful.

regards

vinod