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

Modify File Content

Former Member
0 Likes
920

Hi,

I want to read the line by line of a CSV file and replace string1 in the line with string2 in the same line and update the file.

i have the following code,

but the below code will add a new line with the replaced string, instead i want to modify the same line instead of adding a new line.

OPEN DATASET chq_ip_file_path for UPDATE in text mode encoding default.

WHILE sy-subrc = 0.

READ DATASET chq_ip_file_path INTO xtext.

IF sy-subrc = 0.

split xtext at ';' into xone xtwo xthree xfour.

if xthree <> 'CCAHIER' and xthree(3) = 'CCA'.

REPLACE ALL OCCURRENCES OF '0ACCOUNT'

IN xtext WITH 'ICSKOBLA'.

TRANSFER xtext TO chq_ip_file_path.

endif.

ELSE.

EXIT.

ENDIF.

ENDWHILE.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
458

Hello

You have to switch the cursor position when updating the dataset.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_DATASET_UPDATE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_dataset_update.


CONSTANTS:
  gc_file    TYPE string  VALUE 'dataset_update.txt'.


DATA:
  gd_string    TYPE string,
  gd_pos_old   TYPE i,
  gd_pos_curr  TYPE i.


START-OF-SELECTION.


  DELETE DATASET gc_file.

  OPEN DATASET gc_file FOR OUTPUT IN TEXT MODE
                                  ENCODING DEFAULT.
  gd_string = 'MODIFY;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.
  gd_string = 'REPLACE;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.
  gd_string = 'DISPLAY;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.

  CLOSE DATASET gc_file.
* NOTE: create sample dataset with three lines


  OPEN DATASET gc_file FOR UPDATE IN TEXT MODE
                                  ENCODING DEFAULT.

  WHILE ( syst-subrc = 0 ).
*   Store old cursor position before reading line
    GET DATASET gc_file POSITION gd_pos_old.
    READ DATASET gc_file INTO gd_string.
    IF ( syst-subrc = 0 ).
*     Store current cursor position after reading line
      GET DATASET gc_file POSITION gd_pos_curr.

      IF ( gd_string CS 'REPLACE' ).
        REPLACE ALL OCCURRENCES OF 'T' IN gd_string WITH '?'.

*       Set cursor to old position to overwrite line
        SET DATASET gc_file POSITION gd_pos_old.
        TRANSFER gd_string TO gc_file.
*       Set cursor back to last (= current) position
        SET DATASET gc_file POSITION gd_pos_curr.
      ENDIF.

    ENDIF.
  ENDWHILE.
  CLOSE DATASET gc_file.

END-OF-SELECTION.

Regards

Uwe

1 REPLY 1
Read only

uwe_schieferstein
Active Contributor
0 Likes
459

Hello

You have to switch the cursor position when updating the dataset.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_DATASET_UPDATE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_dataset_update.


CONSTANTS:
  gc_file    TYPE string  VALUE 'dataset_update.txt'.


DATA:
  gd_string    TYPE string,
  gd_pos_old   TYPE i,
  gd_pos_curr  TYPE i.


START-OF-SELECTION.


  DELETE DATASET gc_file.

  OPEN DATASET gc_file FOR OUTPUT IN TEXT MODE
                                  ENCODING DEFAULT.
  gd_string = 'MODIFY;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.
  gd_string = 'REPLACE;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.
  gd_string = 'DISPLAY;THIS;TEXT'.
  TRANSFER gd_string TO gc_file.

  CLOSE DATASET gc_file.
* NOTE: create sample dataset with three lines


  OPEN DATASET gc_file FOR UPDATE IN TEXT MODE
                                  ENCODING DEFAULT.

  WHILE ( syst-subrc = 0 ).
*   Store old cursor position before reading line
    GET DATASET gc_file POSITION gd_pos_old.
    READ DATASET gc_file INTO gd_string.
    IF ( syst-subrc = 0 ).
*     Store current cursor position after reading line
      GET DATASET gc_file POSITION gd_pos_curr.

      IF ( gd_string CS 'REPLACE' ).
        REPLACE ALL OCCURRENCES OF 'T' IN gd_string WITH '?'.

*       Set cursor to old position to overwrite line
        SET DATASET gc_file POSITION gd_pos_old.
        TRANSFER gd_string TO gc_file.
*       Set cursor back to last (= current) position
        SET DATASET gc_file POSITION gd_pos_curr.
      ENDIF.

    ENDIF.
  ENDWHILE.
  CLOSE DATASET gc_file.

END-OF-SELECTION.

Regards

Uwe