Xiao Qing
作者Xiao Qing·2022-12-14 17:19
系统工程师·浪潮商用机器有限公司

如何用RPG语言访问AS/400的IFS文件系统?

字数 5162阅读 4991评论 0赞 4

AS/400 中 RPG 语言的优势是对数据库文件进行输入输出操作,但 RPG 语言不能利用自身的输入输出函数来操作 IFS 集成文件系统中的文件。

关于 IFS 文件的概念,大家可以参考“用 ILE C 语言将数据写入 AS/400 集成文件系统 root ( / )”,文章链接如下 https://www.talkwithtrend.com/Article/260863

AS/400支持的文件系统名称描述
1Library (QSYS.LIB)IBM i library structure,AS/400 native file system
2Open Systems (QOpenSys)UNIX-based open system
3root (/)DOS and OS/2 file systems
4Document Library Services (QDLS)Supports the folder objects. It provides access to documents and folders
5LAN Server/400 (QLANSrv)OS/2 Warp Server for AS/400 file system
6Optical Support (QOPT)Optical Support (QOPT) file system
7File Server (QFileSvr.400)The File Server (QFileSvr.400) file system

如上图所示, AS/400 除了支持自身的 Library(QSYS.LIB) 以外,还支持另外六种文件系统以支持相应的文件系统。

那么 RPG 有没有办法实现对 IFS 文件的访问呢?回答是肯定的,可以利用 AS/400 提供的 C 标准库和服务程序( Service Programs )中预编译好的专用程序对 IFS 文件进行访问 , AS/400 平台 上的所有开发语言都可以通过原型调用和绑定来使用这些专用程序。

为了便于使用, AS/400 将所用到的服务程序绑定在一起,绑定的目录名为 QSYS/QC2LE ,可以用 WRKBNDDIRE QSYS/QC2LE 命令查看绑定的目录条目内容。

每个条目对应一个服务程序,例如:

· QC2SYS - Access to execute system (CL) commands

· QC2UTIL1/2/3 - Some utility and C standard library procedures

· QC2POSIX - General POSIX procedures

· QC2IO - Input and Output related procedures

可以用 DSPSRVPGM 命令查看服务程序的 Procedure exports 和 Data exports 。

例如, QC2IO 是与输入输出程序相关的服务程序 , 若要对文件进行输入输出操作,可以调用此服务程序中的 procedure 。

· Procedure exports

DSPSRVPGM SRVPGM(QSYS/QC2IO) DETAIL(*PROCEXP)

· Data exports

DSPSRVPGM SRVPGM(QSYS/QC2IO) DETAIL(*DTAEXP)

下面通过一个例子来说明如何用 RPG 访问 IFS 文件系统。

 *************************************************************
      *  Write data in ISF stream file with RPG                   *
      *                                                           *
      *      Edited by Xiao Qing                                  *
      *                                      2022/12                                            *
      *                                                           *
      *************************************************************
     H DFTNAME(IFSWRITE) DATEDIT(*YMD/) BNDDIR('QC2LE')
      /COPY QSYSINC/QRPGLESRC,IFS
     D FD              S             10I 0
     D RC              S             10I 0
     D RC1             S             10I 0
     D EBCDIC          S             10I 0 INZ(1388)
     D ASC             S             10I 0 INZ(819)
     D DATA            S             50A
       /FREE
      *************************************************************
      *  Create and open a TXT file in IFS                        *
      *                                                           *
      *************************************************************
         FD = open('/xiaoqing/hello.txt': O_CREAT + O_TRUNC + O_WRONLY +
         O_CCSID : O_RDWR : EBCDIC);
         dsply ('Error code is ' + %char(FD));
         IF (FD < 0 );
           dsply ('open() for create failed.');
           return;
         ENDIF;
         DATA = '你好IPS!';
      *************************************************************
      *  Input data in the TXT file                               *
      *                                                           *
      *************************************************************
         RC=write(FD: %ADDR(DATA): %LEN(%TRIMR(DATA)));
         dsply ('Error code RC is ' + %char(RC));
         if ( RC = -1 );
            dsply ('Write failed');
            return;
         endif;
      *************************************************************
      *  Close the TXT file                                                    *
      *                                                           *
      *************************************************************
         rc1 = close(FD);
         dsply ('Error code RC1 is ' + %char(RC1));
         if ( RC1 <> 0 );
            dsply ('close failed');
            return;
         endif;
 
        *INLR = *ON;
       /END-FREE
 

说明:

H DFTNAME(IFSWRITE) DATEDIT(*YMD/) BNDDIR('QC2LE')

H 规范表中 BNDDIR('QC2LE') 的用途是绑定目录 QC2LE 服务程序以便 RPG 程序可以调用其中的 procedure 。

FD = open('/xiaoqing/hello.txt': O_CREAT + O_TRUNC + O_WRONLY +
O_CCSID : O_RDWR : EBCDIC);

open 的参数解释如下:

O_CREAT

If the file being opened does not exist, it is created and then opened.

O_TRUNC

Truncate the file to zero length if the file exists and it is a "regular file" (a stream file that can support positioning the file offset). The mode and owner of the file are not changed. O_TRUNC applies only to regular files. O_TRUNC has no effect on FIFO special files. The O_TRUNC behavior applies only when the file is successfully opened with O_RDWR or

O_WRONLY.

Open for writing only.

O_CCSID

The call to open has a fourth argument (conversion ID), which is to be interpreted as a CCSID. Text conversions between any two CCSIDs supported by the iconv() API can be performed.

O_WRONLY

Open for writing only.

D EBCDIC S 10I 0 INZ(1388)

在变量定义中制定了 EBCDIC 为 1388 ,所以 CCSID 为 1388 的流文件 /xiaoqing/hello.txt 被创建 , 可以用 WRKLNK 命令 , 然后 option 2 查看流文件的 CCSID 值。


有关更详细的介绍,大家可以参考 IBM 的官网链接 https://www.ibm.com/docs/en/i/7.3?topic=ssw_ibm_i_73/apis/open.html

最后,执行一下这个程序检查一下执行结果。

在 AS/400 命令行执行 call xqlib/ifswrite


程序执行成功后会在 '/xiaoqing' 目录中生成 'hello.txt' 的文件。


然后用 '5=Display' 打开这个文件,查看内容。

仅供参考

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

4

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

X社区推广