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:Β 

Unable to read a file

Former Member
0 Kudos

Hi,

I want to read a file from the local PC and print the content on the screen.

Here is the code below.

DATA : filename LIKE ibipparms-path,

rec(80).

CALL FUNCTION 'F4_FILENAME'

EXPORTING

program_name = syst-cprog

dynpro_number = syst-dynnr

  • FIELD_NAME = ' '

IMPORTING

file_name = filename.

WRITE πŸ˜• filename.

OPEN DATASET filename IN TEXT MODE.

IF sy-subrc = 0.

DO.

READ DATASET filename INTO rec.

IF sy-subrc <> 0.

EXIT.

ELSE.

WRITE / rec.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET filename.

Can anybody help me ?

I am unable to the content.

Thanks.

Jeetu

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi have faced similar scenario and this is my code.

selection-screen begin of block b1 with frame .
PARAMETER : FILE(100) lower case.
selection-screen end of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
*     DEF_FILENAME           = ' '
      DEF_PATH               = '*.*'
*     MASK                   = ' '
*     MODE                   = ' '
*     TITLE                  = ' '
    IMPORTING
      FILENAME               = FILE
*     RC                     =
           .
data file1 type string.
file1 = file.
CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        FILENAME = file1
      TABLES
        DATA_TAB = ti_recORD[].
end-of-selection.
loop at ti_record.
        write:/ ti_record.
      endloop.

6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Do not use the DATASET statements, use the function module GUI_UPLOAD.

Regards,

Rich Heilman

0 Kudos

Here is a sample program which uploads a comma delimited text file and prints the contents.




report zrich_0001.

types: begin of ttab,
       rec(1000) type c,
       end of ttab.

types: begin of tdat,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end of tdat.

data: itab type table of ttab with header line.
data: idat type table of tdat with header line.

data: file_str type string.

parameters: p_file type localfile.

at selection-screen on value-request for p_file.
  call function 'KD_GET_FILENAME_ON_F4'
       exporting
            static    = 'X'
       changing
            file_name = p_file.

start-of-selection.

  file_str = p_file.

  call function 'GUI_UPLOAD'
       exporting
            filename                = file_str
       tables
            data_tab                = itab
       exceptions
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            others                  = 17.


  loop at itab.
    clear idat.
    split itab-rec at ',' into idat-fld1
                               idat-fld2
                               idat-fld3.
    append idat.

  endloop.


  loop at idat.
    write:/ idat-fld1, idat-fld2, idat-fld3.
  endloop.

You should only use the DATASET statements when working with files on the application server. When working with files on the presentation or PC, then use GUI_UPLOAD, or the GUI_UPLOAD method of the class CL_GUI_FRONTEND_SERVICES.

Please make sure to award points for helpful answers and mark your post as solved when solved completely. Thanks.

Regards,

Rich Heilman

former_member188685
Active Contributor
0 Kudos

Hoe can you read the fiel using datasets.

use Gui_upload function to do that...

vijay

former_member188685
Active Contributor
0 Kudos
DATA : filename LIKE ibipparms-path,
rec(80).

CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
* FIELD_NAME = ' '
IMPORTING
file_name = filename.
WRITE πŸ˜• filename.

CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename                = filename
        filetype                = 'ASC'
        has_field_separator     = ''
      IMPORTING
        filelength              = l_filelength
      CHANGING
        data_tab                = data_tab
      EXCEPTIONS
        file_open_error         = 1
        file_read_error         = 2
        no_batch                = 3
        gui_refuse_filetransfer = 4
        invalid_type            = 5
        no_authority            = 6
        unknown_error           = 7
        bad_data_format         = 8
        header_not_allowed      = 9
        separator_not_allowed   = 10
        header_too_long         = 11
        unknown_dp_error        = 12
        access_denied           = 13
        OTHERS                  = 14.

    IF sy-subrc NE 0.

    ENDIF.

    LOOP AT data_tab .

    ENDLOOP.

Check this..

Former Member
0 Kudos

Hi have faced similar scenario and this is my code.

selection-screen begin of block b1 with frame .
PARAMETER : FILE(100) lower case.
selection-screen end of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
*     DEF_FILENAME           = ' '
      DEF_PATH               = '*.*'
*     MASK                   = ' '
*     MODE                   = ' '
*     TITLE                  = ' '
    IMPORTING
      FILENAME               = FILE
*     RC                     =
           .
data file1 type string.
file1 = file.
CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        FILENAME = file1
      TABLES
        DATA_TAB = ti_recORD[].
end-of-selection.
loop at ti_record.
        write:/ ti_record.
      endloop.

0 Kudos

Check if the values which you are getting for filename variable is same as the file name you have kept. Generally this problem is due to conversion to upper case and the program doesn't find any filename. So either save your file with capital letters or convert the variable back to lower case.

Regards,

Ankur Bhandari.

p.s Reward if it helps.