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: 

OUTPUT PROBLEM FOR OPEN DATASET

Former Member
0 Kudos

Hi all,

I tried the program with the sample in SAP library, the file is created with the content displayed on the screen, but when I open the myfile.txt, the content is blank. I want to write content in the text file, but it seems failed.

What is the problem?

Thanks.

http://help.sap.com/saphelp_47x200/helpdata/EN/fc/eb3db7358411d1829f0000e829fbfe/frameset.htm

-


DATA FNAME(60) VALUE 'myfile.txt'.

TYPES: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

TYPES ITAB TYPE LINE OCCURS 10.

DATA: LIN TYPE LINE,

TAB TYPE ITAB.

DO 5 TIMES.

LIN-COL1 = SY-INDEX.

LIN-COL2 = SY-INDEX ** 2.

APPEND LIN TO TAB.

ENDDO.

OPEN DATASET FNAME FOR OUTPUT.

LOOP AT TAB INTO LIN.

TRANSFER LIN TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT.

DO.

READ DATASET FNAME INTO LIN.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

WRITE: / LIN-COL1, LIN-COL2.

ENDDO.

CLOSE DATASET FNAME.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Here is the problem...

OPEN DATASET FNAME FOR OUTPUT.

LOOP AT TAB INTO LIN.

TRANSFER LIN TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

before doing a transfer you have to check sy-subrc for open dataset to find out whether dataset 'myfile.txt' was successfully opened or not. I think open dataset is failing because the file path that is specified.

Please do a check after open dataset then verify.

Hope this helps.

Sharath.

6 REPLIES 6

Former Member
0 Kudos

Hi,

Here is the problem...

OPEN DATASET FNAME FOR OUTPUT.

LOOP AT TAB INTO LIN.

TRANSFER LIN TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

before doing a transfer you have to check sy-subrc for open dataset to find out whether dataset 'myfile.txt' was successfully opened or not. I think open dataset is failing because the file path that is specified.

Please do a check after open dataset then verify.

Hope this helps.

Sharath.

0 Kudos

Hi Sharath,Christian

Thanks for your reply.

The file path is correct because I could find that myfile.txt is created in the path after executing the program.

I try to add some text to the myfile.txt directly in notepad, and see if the program could read the text in it.

The program could read the text. But just can't write text to it.

And I've checked that SY-SUBRC is = 0, and I have authorization to write in the directory that I specified (I don't put the path as that of application server, I just put the path on my server, e.g.
xxx\temp\).

Could you tell what other possibilities are there?

0 Kudos

Hi Macy,

looks like nothing is wrong. This program defines an internal table, fill this table with two numbers (line number and square of line number), <b>writes this internal table into a file</b>, re-reads this file into internal table, printout of internal table.

If you manually change myfile.txt, it's getting overwritten in the first part of the program - that's all.

Just deactivate the coding from 'do 5 times' up to the first 'close dataset' - then you can show own changes of myfile.txt.

Regards,

Christian

0 Kudos

Hi,

try that:

DATA mess(100).
OPEN DATASET fname FOR OUTPUT IN TEXT MODE MESSAGE mess.
IF sy-subrc <> 0.
  WRITE / mess.
  EXIT.
ENDIF.
DATA: BEGIN OF wa , col1(15), col2(15), END OF wa.
LOOP AT tab INTO lin.
   move-corresponding lin to wa.
  TRANSFER wa TO fname.
ENDLOOP.

regards Andreas

christian_wohlfahrt
Active Contributor
0 Kudos

Hi Macy,

maybe it's just a stupid authority problem.

It's a small program, just debug (after OPEN DATASET) and check, if somewhere SY-SUBRC <> 0 appears.

In help of corresponding command you will find a hint for your problem.

Quite often users don't have write rights on application server's file system.

Regards,

Christian

Former Member
0 Kudos

Hi Macy,

I tried to execute your program .It is working perfectly for me . I added 'IN BINARY MODE ' along with the OPEN DATASET.

DATA fname(60) VALUE 'myfile.txt'.

TYPES: BEGIN OF line,

col1 TYPE i,

col2 TYPE i,

END OF line.

TYPES itab TYPE line OCCURS 10.

DATA: lin TYPE line,

tab TYPE itab.

DO 5 TIMES.

lin-col1 = sy-index.

lin-col2 = sy-index ** 2.

APPEND lin TO tab.

ENDDO.

OPEN DATASET fname FOR OUTPUT IN BINARY MODE.

LOOP AT tab INTO lin.

TRANSFER lin TO fname.

ENDLOOP.

CLOSE DATASET fname.

OPEN DATASET fname FOR INPUT IN BINARY MODE.

DO.

READ DATASET fname INTO lin.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: / lin-col1, lin-col2.

ENDDO.

CLOSE DATASET fname.