Application Development and Automation 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: 
Read only

Working with File

Former Member
0 Likes
1,020

Hi all,

Can any one tell me how to open ,write and close on SAP file on App.Server

Thanks Advance.

Ankur Garg.

Hi all,

Can any one tell me how to open ,write and close on SAP file on App.Server

Thanks Advance.

Ankur Garg.

7 REPLIES 7
Read only

Former Member
0 Likes
980

Hi,

Use dataset

<b>Check this link</b>

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/open_dat.htm

OPEN DATASET

Basic form 1

OPEN DATASET dsn.

Extras:

1a. ... FOR INPUT

1b. ... FOR OUTPUT

1c. ... FOR APPENDING

1d. ... FOR UPDATE

2a. ... IN BINARY MODE

2b. ... IN TEXT MODE [ENCODING (DEFAULT|UTF-8|NON-UNICODE)]

2c. ... IN LEGACY BINARY MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]

2d. ... IN LEGACY TEXT MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]

3. ... REPLACEMENT CHARACTER rc

4. ... IGNORING CONVERSION ERRORS

5. ... AT POSITION p

6. ... TYPE c

7. ... MESSAGE m

8. ... FILTER f

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. For details see the section File Interface.

Effect

Opens the specified file. If you do not specify a directory, the system uses the directory defined in the profile parameter DIR_HOME.

In programs without active Unicode check, the file is opened for reading in binary mode if you do not use any additions for OPENDATASET. To ensure downward compatibility with Releases <= 4.6, file names containing blanks are truncated at the position of the first blank.

In programs with active Unicode check, you must specify the access type (such as ... FOR INPUT, ... FOR OUTPUT, and so on) and the mode (such as ... IN TEXT MODE, ... IN BINARY MODE, and so on). If the file is opened using ... IN TEXT MODE, you must still use the addition ... ENCODING. If the Unicode check is enabled, it is possible to use file names containing blanks. Applying OPEN DATASET to a file already opened - in the same internal mode - triggers an exception of the type CX_SY_FILE_OPEN.

The Return Code is set as follows:

SY-SUBRC = 0:

The file was opened.

SY-SUBRC = 8:

The file could not be opened.

Example

DATA:

dsn(20) TYPE C VALUE '/usr/test.dat',

rec(80) TYPE C.

OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc <> 0.

EXIT.

ENDIF.

READ DATASET dsn INTO rec.

WHILE sy-subrc <> 0.

WRITE / rec. READ DATASET dsn INTO rec.

ENDWHILE.

CLOSE DATASET dsn.

Read only

0 Likes
980

First you get your data into an internal table.

Then copy the following code.

FORM write_to_file.

                                    • Setting the file name ************************

CONCATENATE 'zfiapimg_' sy-datum '.txt' INTO wa_filename.

wa_filepath = '/data/ftp/out/'.

CONCATENATE wa_filepath wa_filename INTO wa_c_filename.

                                    • Open the file for output **********************

OPEN DATASET wa_c_filename FOR OUTPUT IN TEXT MODE.

IF sy-subrc NE 0.

MESSAGE a999 WITH 'The file could not be created'.

ENDIF.

                                • Write header data to file ************************

TRANSFER header TO wa_c_filename.

                                • Write line items to file ************************

LOOP AT i_final_file.

TRANSFER i_final_file TO wa_c_filename.

ENDLOOP.

                                          • Close the file ******************************

CLOSE DATASET wa_c_filename.

ENDFORM.

You will have the contents in the file name and file path as specified.

Read only

oliver
Active Contributor
0 Likes
980

Hi Ankur,

ABAP has the Keywords OPEN/READ/CLOSE DATASET for serverbased file actions.

Here is an example:

open dataset w_fname for input in text mode " get input data

message lw_msg. " any errors place here

if sy-subrc <> 0.

message i002 with lw_msg.

stop.

endif.

read dataset w_fname into lw_row.

if sy-subrc <> 0. " end of file reached

exit. " leave do loop

endif.

loop at lt_files.

delete dataset lt_files-name.

if sy-subrc = 0.

t_body-line = lt_files-name.

append t_body.

endif.

endloop.

form write_unix_file.

open dataset myfile for output in text mode.

if sy-subrc <> 0.

write sy-subrc to w_subrc.

concatenate 'Problem opening' myfile 'to UNIX.' w_subrc

into t_body-line separated by space.

append t_body.

concatenate p_maber 'Invoice Load problems encountered'

into w_subject.

perform send_mail.

message a000 with 'Problem opening UNIX.' sy-subrc." stop processing because transfer will short dump

endif. "problem with open

loop at t_invoice.

transfer t_invoice to myfile.

endloop. "loop through invoices

close dataset myfile.

For further details take a look into your ABAP online help for keyword DATASET.

Hope that helps,

Oliver

Read only

Former Member
0 Likes
980

Hi,

You can do that with the help of OPEN DATASET(to open the file for open an existing or to create new one), READ DATASET (to read the file), Transafer to transfer the content, Close dataset to close the file.

this will write the content of itab to application server.

open dataset ds for output in text mode endcoding default.

if sy-subrc  = 0.

loop at itab.
transfer itab to ds.
endloop.
endif. 

close dataset ds.

Regards

vijay

Read only

Former Member
0 Likes
980

Hai

Check the following

OPEN DATASET

Basic form 1

OPEN DATASET dsn.

Extras:

1a. ... FOR INPUT

1b. ... FOR OUTPUT

1c. ... FOR APPENDING

1d. ... FOR UPDATE

2a. ... IN BINARY MODE

2b. ... IN TEXT MODE [ENCODING (DEFAULT|UTF-8|NON-UNICODE)]

2c. ... IN LEGACY BINARY MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]

2d. ... IN LEGACY TEXT MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]

3. ... REPLACEMENT CHARACTER rc

4. ... IGNORING CONVERSION ERRORS

5. ... AT POSITION p

6. ... TYPE c

7. ... MESSAGE m

8. ... FILTER f

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. For details see the section File Interface.

Effect

Opens the specified file. If you do not specify a directory, the system uses the directory defined in the profile parameter DIR_HOME.

In programs without active Unicode check, the file is opened for reading in binary mode if you do not use any additions for OPENDATASET. To ensure downward compatibility with Releases <= 4.6, file names containing blanks are truncated at the position of the first blank.

In programs with active Unicode check, you must specify the access type (such as ... FOR INPUT, ... FOR OUTPUT, and so on) and the mode (such as ... IN TEXT MODE, ... IN BINARY MODE, and so on). If the file is opened using ... IN TEXT MODE, you must still use the addition ... ENCODING. If the Unicode check is enabled, it is possible to use file names containing blanks. Applying OPEN DATASET to a file already opened - in the same internal mode - triggers an exception of the type CX_SY_FILE_OPEN.

The Return Code is set as follows:

SY-SUBRC = 0:

The file was opened.

SY-SUBRC = 8:

The file could not be opened.

Go through the following Example

data: v_hex type x,

v_bit type n.

open dataset file_name for input in binary mode encoding default.

do.

read dataset file_name into v_hex.

if sy-subrc ne 0.

close dataset file_name

exit.

endif

do 8 times.

get bit sy-index of v_hex into v_bit.

write v_bit no-gap.

enddo.

enddo.

Thanks & regards

Sreeni

Read only

Former Member
0 Likes
980

Hi Ankur,

Please check help of following commands :

1. OPEN DATASET ( To open file in application server )

2. TRANSFER ( Write text to file )

3. READ DATASET ( Read from file )

4. CLOSE DATASET ( Close file in application server )

Regards,

Naren

Read only

Former Member
0 Likes
980

Hi,

whenever u want to open a file for Reading (Input) or Writing (output) to a file .....

use the statement

Open Dataset <Datasetname> for [input or output] in [Textmode or Binary Mode] encoding Default.

for example u declare as this

Data : File_09(20) type c.

File_09 is a Dataset in other terms its a Temporary buffer in which ur file is stored in application server ...

u assign some file to this ... say

File_09 = 'C:\DATA_01.TXT'.

this file path is just an example the file however is created in Appl. Server ok...

then u open that dataset which points to the file C:\DATA_01.TXT

open Dataset File_09 for ouput in text mode encoding default .

this says that u want to open a file pointed out by or referneced by file_09 for writing something into that file in text mode. ok.

suppose u have declared a internal table itab which u want to write to the file, then use ...

loop at itab.

Transfer itab to file_09.

endloop.

then u need to close the dataset using

close dataset file_09.

Regards,

Sangeeta.