tnt400.com - AS/400 Tips And Techniques

Sponsored by news400.com

This page is a discussion on the mentioned topic.
Most of the answers are in their original posted form, including any technical/spelling/grammatical errors.
No guarantees are expressed or implied. :-)
Comments, corrections, concerns about this tip?

Got another AS/400 question? Ask it here


What's New?
See what's new at Tips-N-Tech.

AS/400 Tips-N-Tech
AS/400 tips, techniques, and FAQ. Updated frequently.
CODEPage/400
All the code samples you can eat! RPG, CL, DDS, etc. etc.
AS/400 FAQs
The official news400.com FAQs.






All AS/400 Tip Categories / AS/400 Programming / Using IFS APIs w/ILE RPG


Question:

Sometime back, someone posted the correct way to write data to an IFS file using ILE RPG and the IFS write() API. I didn't bother looking at it because I use the QDCXLATE program to translate from EBCDIC to ASCII before writing. Now, I want to read from an IFS file (using read()) and then write a different file back out. Most IFS files that I read work fine but I've run into one from an outside vendor that doesn't work. The file is in ASCII, but when I write it back out, it gets translated back to EBCDIC. I verified this by converting the data using QDCXLATE before writing back out. Does anyone have the correct way of doing this? It had something to do with the codepage parameter.


Answer(s):

Here is some code for writing and reading files to the IFS system. My understanding was you had to open the file with create first so that the conversion with the code page will work properly. I know the following code works good.

D RC              S             10I 0
D FileNam         S             50A   INZ('/qualcomm/test1'
D FileNamP        S               *   INZ(%ADDR(FileNam))
D FileDescr       S             10I 0

D O_CREAT         S             10I 0 INZ(8)
D O_RDWR          S             10I 0 INZ(4)
D O_TEXTDATA      S             10I 0 INZ(16777216)
D O_CODEPAGE      S             10I 0 INZ(8388608)
D Oflag           S             10I 0 INZ(0)
D Omode           S             10U 0 INZ(511)
D cp              S             10U 0 INZ(819)

D ZeroBin         S              1A   INZ(*ALLX'00')
D NLZero          S              2A   INZ(X'1500')
D SI_Fmt          S             50A   INZ('\n')
D SI_FmtP         S               *   INZ(%ADDR(SI_Fmt))
D SI_Msg          S             50A
D SI_MsgP         S               *   INZ(%ADDR(SI_Msg))

D Num_DS          DS
D Num_Hex                        4A   INZ(X'00000000')
D Num                           10I 0 OVERLAY(Num_Hex)

D Buf             S            100A
D BufP            S               *   INZ(%ADDR(Buf))
D BufLen          S             10U 0

D/copy IFSHEAD

C                   EVAL      FileNam = %TRIM(FileNam) + ZeroBin

C                   Z-add     O_CREAT       Oflag
C                   Add       O_RDWR        Oflag
C                   Add       O_CODEPAGE    Oflag

C                   EVAL      FileDescr=open(FileNamP:Oflag:Omode:cp)

C                   IF        FileDescr = -1
C                   EVAL      RC = perror(FileNamP)
C                   Return
C                   ENDIF

C                   EVAL      RC = close(FileDescr)

C                   IF        RC = -1
C                   EVAL      RC = perror(FileNamP)
C                   Return
C                   ENDIF

C                   Z-Add     O_RDWR        Oflag
C                   Add       O_TEXTDATA    Oflag

C                   EVAL      FileDescr=open(FileNamP:Oflag)

C                   IF        FileDescr = -1
C                   EVAL      RC = perror(FileNamP)
C                   Return
C                   ENDIF

C                   EVAL      Buf='This is a Test number 1' + X'25'
C     X'25'         SCAN      Buf           BufLen                   30
C                   EVAL      RC = write(FileDescr: BufP: BufLen)

C                   IF        RC = -1
C                   EVAL      RC = perror(FileNamP)
C                   Return
C                   ENDIF

 * Close the File
C                   EVAL      RC = close(FileDescr)

C                   IF        FileDescr = -1
C                   EVAL      RC = perror(FileNamP)
C                   Return
C                   ENDIF

The following is the IFSHEAD file that I copy in.. There are defined routines here that I use in other programs .. I just keep them all together as I create new headers for C functions..

Dperror           PR            10I 0 EXTPROC('perror')
Dconst                            *   VALUE

Dsprintf          PR            10I 0 EXTPROC('sprintf')
D                                 *   VALUE
D                                 *   VALUE
D                               10I 0 VALUE OPTIONS(*NOPASS)
D                                 *   VALUE OPTIONS(*NOPASS)

 * Open Operations
 * value returned = file descriptor 0 (OK), -1 (Error)

Dopen             PR            10I 0 EXTPROC('open')
D                                 *   VALUE
D                               10I 0 VALUE
D                               10U 0 VALUE OPTIONS(*NOPASS)
D                               10U 0 VALUE OPTIONS(*NOPASS)

 * Read Operations
 * value returned = number of bytes read or , -1 (Error)

Dread             PR            10I 0 EXTPROC('read')
D                               10I 0 VALUE
D                                 *   Value
D                               10U 0 VALUE

 * Write Operations
 * value returned = number of bytes Written or , -1 (Error)

Dwrite            PR            10I 0 EXTPROC('write')
D                               10I 0 VALUE
D                                 *   VALUE
D                               10U 0 VALUE

 * Close Operations
 * value returned = 0 (OK) or , -1 (Error)

Dclose            PR            10I 0 EXTPROC('close')
D                               10I 0 VALUE

 * Open Directory Operation
 * value returned = file descriptor 0 (OK), -1 (Error)

Dopendir          PR              *   EXTPROC('opendir')
D                                 *   VALUE

 * Read Directory Operation
 *


Dreaddir          PR              *   EXTPROC('readdir')
D                                 *   VALUE

 * Open Directory Operation
 * value returned = 0 (OK) or , -1 (Error)

Dclosedir         PR            10I 0 EXTPROC('closedir')
D                                 *   VALUE

 * Unlink a File from system... Delete File
 * value returned = 0 (OK) or , -1 (Error)

Dunlink           PR            10I 0 EXTPROC('unlink')
D                                 *   VALUE

I had to do some research to make it work. After a few hours I found UNISTD.H in QSYSINC which tells me I have to setup an environment variable of QIBM_USE_DESCRIPTOR_STDIO and make it's value "Y" to get the reserved file descriptors. I also had to dig to figure out that I have to bind to service program QC2LE in QSYS to take advantage of SPRINTF and PERROR.

<Ed Davidson, via mailing list, 1/2000>

 


You are at a news400.com site.
Contact Us | Report Bugs | Submit Comments/Suggestions | Read Site Use Agreement | Read Privacy Policy
Copyright © 2000 Duke Communications International.
This site is best viewed with the latest versions of Netscape or Internet Explorer, 800 x 600 resolution (or higher), and at least 256 colors.
Duke Communications   NEWS/400 | 29th Street Press | Business Finance | DominoPro | Selling AS/400 Solutions | SQL Server Magazine | Windows NT Magazine