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

formatting the file

Former Member
0 Likes
689

Hi folks,

I have a question, need to format the file that I generate through the logical file path. i.e., The file has the data for various field values, however I need get the field names displayed at the start of the file.

I did something like this

itab -EEN = 'Emp ID'.

itab-LNAME = 'Last Name'.

itab-FNAME = 'First Name'.

itab-SSN = 'SSN'.

APPEND ITAB.

CLEAR ITAB.

then loop through the internal table that had data for these fields and stored the data

itab -EEN = ' 2348907'.

itab-LNAME = ' XYZ'.

itab-FNAME = 'ABC'.

itab-SSN = ' 011224567'

APPEND ITAB.

CLEAR ITAB.

*****************

DESCRIBE TABLE ITAB LINES COUNTK.

IF COUNTK GT 0.

SORT ITAB BY SSN.

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = 'EFGH'

  • WITH_FILE_EXTENSION = 'X'

IMPORTING

FILE_NAME = FNAME.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE MESSAGE msg.

IF sy-subrc <> 0.

WRITE / msg.

STOP.

ENDIF.

LOOP AT ITAB.

TRANSFER ITAB TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

The file is getting generated with the values first and then the text and I want the other way around. How can i do that?

Thanks in advance,

Sk

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
663

The SORT statement is definitly causing your problem, but if you want to sort by SSN in the file, then you must add the field names after the fact.



* Add detail records
itab-EEN = ' 2348907'.
itab-LNAME = ' XYZ'.
itab-FNAME = 'ABC'.
itab-SSN = ' 011224567'
APPEND ITAB.
CLEAR ITAB.


* Check for line count
DESCRIBE TABLE ITAB LINES COUNTK.

IF COUNTK GT 0.

* SOrt by SSN
SORT ITAB BY SSN.


* Add your Field names now, using the INSERT statement.
itab-EEN = 'Emp ID'.
itab-LNAME = 'Last Name'.
itab-FNAME = 'First Name'.
itab-SSN = 'SSN'.
INSERT ITAB index 1.
CLEAR ITAB.

* Get the file
CALL FUNCTION 'FILE_GET_NAME'
        EXPORTING
             LOGICAL_FILENAME = 'EFGH'
*           WITH_FILE_EXTENSION = 'X'
          IMPORTING
            FILE_NAME = FNAME.

* Open and Write.
OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE MESSAGE msg.
IF sy-subrc <> 0.
   WRITE / msg.
   STOP.
ENDIF.

LOOP AT ITAB.
    TRANSFER ITAB TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.

Regards,

RIch Heilman

Hi folks,

I have a question, need to format the file that I generate through the logical file path. i.e., The file has the data for various field values, however I need get the field names displayed at the start of the file.

I did something like this

itab -EEN = 'Emp ID'.

itab-LNAME = 'Last Name'.

itab-FNAME = 'First Name'.

itab-SSN = 'SSN'.

APPEND ITAB.

CLEAR ITAB.

then loop through the internal table that had data for these fields and stored the data

itab -EEN = ' 2348907'.

itab-LNAME = ' XYZ'.

itab-FNAME = 'ABC'.

itab-SSN = ' 011224567'

APPEND ITAB.

CLEAR ITAB.

*****************

DESCRIBE TABLE ITAB LINES COUNTK.

IF COUNTK GT 0.

SORT ITAB BY SSN.

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = 'EFGH'

  • WITH_FILE_EXTENSION = 'X'

IMPORTING

FILE_NAME = FNAME.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE MESSAGE msg.

IF sy-subrc <> 0.

WRITE / msg.

STOP.

ENDIF.

LOOP AT ITAB.

TRANSFER ITAB TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

The file is getting generated with the values first and then the text and I want the other way around. How can i do that?

Thanks in advance,

Sk

5 REPLIES 5
Read only

Former Member
0 Likes
663

data v_data(1024) type c.

LOOP AT ITAB.

concatenate itab-fld1 ',' itab-fld2 ',' into v_data.

TRANSFER v_data TO FNAME.

clear v_data.

ENDLOOP.

Read only

former_member194669
Active Contributor
0 Likes
663

Hi,

Remove the line from your code


SORT ITAB BY SSN

aRs

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
664

The SORT statement is definitly causing your problem, but if you want to sort by SSN in the file, then you must add the field names after the fact.



* Add detail records
itab-EEN = ' 2348907'.
itab-LNAME = ' XYZ'.
itab-FNAME = 'ABC'.
itab-SSN = ' 011224567'
APPEND ITAB.
CLEAR ITAB.


* Check for line count
DESCRIBE TABLE ITAB LINES COUNTK.

IF COUNTK GT 0.

* SOrt by SSN
SORT ITAB BY SSN.


* Add your Field names now, using the INSERT statement.
itab-EEN = 'Emp ID'.
itab-LNAME = 'Last Name'.
itab-FNAME = 'First Name'.
itab-SSN = 'SSN'.
INSERT ITAB index 1.
CLEAR ITAB.

* Get the file
CALL FUNCTION 'FILE_GET_NAME'
        EXPORTING
             LOGICAL_FILENAME = 'EFGH'
*           WITH_FILE_EXTENSION = 'X'
          IMPORTING
            FILE_NAME = FNAME.

* Open and Write.
OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE MESSAGE msg.
IF sy-subrc <> 0.
   WRITE / msg.
   STOP.
ENDIF.

LOOP AT ITAB.
    TRANSFER ITAB TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.

Regards,

RIch Heilman

Read only

0 Likes
663

Thanks guys. It worked. However, can I format the data further like storing into a table?

SK

Read only

0 Likes
663

sorry. storing the data in the table format.

thanks,

SK