|
Hi Emmanuel, Enclosed are two complete programs:WriteFile can be called with a 32-byte file name and it will create the file, and write the literal "some text" to it. The file will be created as an ASCII (CCSID 819) file. With *RWX authority for you, but only *R public authority.
ReadFile can be called with a 32-byte file name and it will open the file and write its contents to the console.
Points to note:1) In order to display data to the console in program readFile I have included the following, which you don't need in your program: BNDDIR('QC2LE'), printf() and getchar(). 2) When using the open API I pass the address of the variable containing the file name + 2 because the variable is VARYING and the first 2 bytes contain the length. If you don't use a VARYING field then remove the "+ 2" part, but be aware that trailing blanks in a fixed-length field can be interpreted as part of the file name - hence the common use of adding a trailing null to terminate the string.
Here's the code (the IFS constants and some of the prototypes can be found in Scott's IFSIO_H copy member):
writeFile:
**********************************************************************
H DFTACTGRP(*NO) ACTGRP('TEST1')
**********************************************************************
* *ENTRY UEP...
D writeFile pr Extpgm('WRITEFILE')
D file 32a
D writeFile pi
D file 32a
**********************************************************************
D open PR 10I 0 ExtProc('open')
D filename * value options(*string)
D openflags 10I 0 value
D mode 10U 0 value options(*nopass)
D ccsid 10U 0 value options(*nopass)
D txtcreatid 10U 0 value options(*nopass)
D closeFile PR 10I 0 ExtProc('close')
D handle 10I 0 value
D write PR 10I 0 ExtProc('write')
D handle 10I 0 value
D buffer * value
D bytes 10U 0 value
**********************************************************************
D* Reading Only
D O_RDONLY C 1
D* Writing Only
D O_WRONLY C 2
D* Reading & Writing
D O_RDWR C 4
D* Create File if not exist
D O_CREAT C 8
D* Exclusively create
D O_EXCL C 16
D* Assign a CCSID
D O_CCSID C 32
D* Truncate File to 0 bytes
D O_TRUNC C 64
D* Append to File
D O_APPEND C 256
D* Synchronous write
D O_SYNC C 1024
D* Sync write, data only
D O_DSYNC C 2048
D* Sync read
D O_RSYNC C 4096
D* No controlling terminal
D O_NOCTTY C 32768
D* Share with readers only
D O_SHARE_RDONLY C 65536
D* Share with writers only
D O_SHARE_WRONLY C 131072
D* Share with read & write
D O_SHARE_RDWR C 262144
D* Share with nobody.
D O_SHARE_NONE C 524288
D* Assign a code page
D O_CODEPAGE C 8388608
D* Open in text-mode
D O_TEXTDATA C 16777216
D* Allow text translation
D* on newly created file.
D O_TEXT_CREAT C 33554432
D* Inherit mode from dir
D O_INHERITMODE C 134217728
D* Large file access
D* (for >2GB files)
D O_LARGEFILE C 536870912
**********************************************************************
D* owner authority
D S_IRUSR C 256
D S_IWUSR C 128
D S_IXUSR C 64
D S_IRWXU C 448
D* group authority
D S_IRGRP C 32
D S_IWGRP C 16
D S_IXGRP C 8
D S_IRWXG C 56
D* other people
D S_IROTH C 4
D S_IWOTH C 2
D S_IXOTH C 1
D S_IRWXO C 7
**********************************************************************
d fileName s 255a varying
d fileDesc s 10i 0
d fileData s 32a inz('some text')
d rc s 10i 0
**********************************************************************
€
/free
// Set filename...
fileName = %trimR(file);
// Create file as ASCII (CCSID = 819) file...
fileDesc = open( (%addr(fileName) + 2)
: O_CREAT + O_WRONLY + O_TRUNC + O_CCSID
: S_IRWXU + S_IROTH
: 819);
// Close the file...
closeFile(fileDesc);
// Re-open file with job (CCSID = 0 defaults to job) CCSID...
fileDesc = open( (%addr(fileName) + 2)
: O_TEXTDATA + O_WRONLY + O_CCSID
: S_IRWXU + S_IROTH
: 0);
// Write data to stream file - simply pass a variable and its length.
rc = write(fileDesc :%addr(fileData) :%len(%trimR(fileData)));
// Because the file has a CCSID of 819, and you have re-opened it
// to write text to it (O_TEXTDATA) your data will be translated
// FROM your job CCSId TO the file CCSID automatically...
// Close file...
closeFile(fileDesc);
return;
/end-free
*********************************************************************
readFile:
**********************************************************************
H DFTACTGRP(*NO) ACTGRP('TEST1') BNDDIR('QC2LE')
**********************************************************************
* *ENTRY UEP...
D readFile pr Extpgm('READFILE')
D file 32a
D readFile pi
D file 32a
**********************************************************************
D open PR 10I 0 ExtProc('open')
D filename * value options(*string)
D openflags 10I 0 value
D mode 10U 0 value options(*nopass)
D ccsid 10U 0 value options(*nopass)
D txtcreatid 10U 0 value options(*nopass)
D closeFile PR 10I 0 ExtProc('close')
D handle 10I 0 value
D read PR 10I 0 ExtProc('read')
D handle 10i 0 value
D buffer * value
D bytes 10U 0 value
**********************************************************************
* prototypes for displaying data to console...
**********************************************************************
* Print file function...
D printf PR extproc('printf')
D printString * value options(*string)
* Get character from standard input...
D getchar PR 10i 0 extproc('getchar')
D character * value
**********************************************************************
D* Reading Only
D O_RDONLY C 1
D* Writing Only
D O_WRONLY C 2
D* Reading & Writing
D O_RDWR C 4
D* Create File if not exist
D O_CREAT C 8
D* Exclusively create
D O_EXCL C 16
D* Assign a CCSID
D O_CCSID C 32
D* Truncate File to 0 bytes
D O_TRUNC C 64
D* Append to File
D O_APPEND C 256
D* Synchronous write
D O_SYNC C 1024
D* Sync write, data only
D O_DSYNC C 2048
D* Sync read
D O_RSYNC C 4096
D* No controlling terminal
D O_NOCTTY C 32768
D* Share with readers only
D O_SHARE_RDONLY C 65536
D* Share with writers only
D O_SHARE_WRONLY C 131072
D* Share with read & write
D O_SHARE_RDWR C 262144
D* Share with nobody.
D O_SHARE_NONE C 524288
D* Assign a code page
D O_CODEPAGE C 8388608
D* Open in text-mode
D O_TEXTDATA C 16777216
D* Allow text translation
D* on newly created file.
D O_TEXT_CREAT C 33554432
D* Inherit mode from dir
D O_INHERITMODE C 134217728
D* Large file access
D* (for >2GB files)
D O_LARGEFILE C 536870912
**********************************************************************
D* owner authority
D S_IRUSR C 256
D S_IWUSR C 128
D S_IXUSR C 64
D S_IRWXU C 448
D* group authority
D S_IRGRP C 32
D S_IWGRP C 16
D S_IXGRP C 8
D S_IRWXG C 56
D* other people
D S_IROTH C 4
D S_IWOTH C 2
D S_IXOTH C 1
D S_IRWXO C 7
**********************************************************************
* IFS File variables...
d fileName s 255a varying
d fileDesc s 10i 0
d fileData s 80a inz
d char s 1a inz
d rc s 10i 0
**********************************************************************
/free
// Set filename...
fileName = %trimR(file);
// Open file for reading text data...
fileDesc = open( (%addr(fileName) + 2)
: O_TEXTDATA + O_RDWR);
// Read the data into a variable - this could be done in a loop...
// rc will return the number of bytes read - loop while > 0...
rc = read(fileDesc:%addr(fileData):%len(fileData));
dow rc > 0;
rc = read(fileDesc:%addr(fileData):%len(fileData));
printf(fileData);
enddo;
// Flush last line by printing a carriage return...
printf(X'25');
// Invite input from user - to keep the console open
// This is done even if nothing is written to the console...
getchar(%addr(char));
// Close file...
closeFile(fileDesc);
return;
/end-free
*********************************************************************
HTH
Cheers
Larry Ducie
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.