diff --git a/jcl/TXTWKR01.jcl b/jcl/TXTWKR01.jcl index e533f66..52ecc0d 100644 --- a/jcl/TXTWKR01.jcl +++ b/jcl/TXTWKR01.jcl @@ -1,5 +1,5 @@ //TXTWKR01 JOB (GCC),'File I/O Example', -// NOTIFY=@05054,CLASS=A,MSGCLASS=A, +// NOTIFY=@05054,CLASS=A,MSGCLASS=H, // MSGLEVEL=(1,1),REGION=4M,TIME=1440 //* Compile and Go Step //STEP1 EXEC GCCCG,INFILE='@05054.SRCLIB.C(TXTWKR01)' diff --git a/src/TXTWKR01.c b/src/TXTWKR01.c index 6097b81..92e4e44 100644 --- a/src/TXTWKR01.c +++ b/src/TXTWKR01.c @@ -10,35 +10,39 @@ int main() { - FILE *fp; + FILE *fp_read; + FILE *fp_append; char buffer[BUFFER_SIZE]; - /* Open the file in update mode (read and write). */ - /* The filename is "dd:DDNAME" where DDNAME is defined in JCL. */ - fp = fopen("dd:TESTDD", "a+"); - if (fp == NULL) + /* First: open for reading only to dump current content */ + fp_read = fopen("dd:TESTDD", "r"); + if (fp_read == NULL) { - perror("fopen failed."); + perror("fopen read failed"); printf("errno = %d\n", __errno()); return 1; } - /* Read and print existing lines (to SYSOUT). */ printf("Existing content:\n"); - while (fgets(buffer, BUFFER_SIZE, fp) != NULL) + while (fgets(buffer, BUFFER_SIZE, fp_read) != NULL) { printf("%s", buffer); } + fclose(fp_read); - /* After reading to EOF, the file position is at the end, so we can append. */ - fprintf(fp, "Appended from C program!\n"); + /* Second: open for append only to add the new line */ + fp_append = fopen("dd:TESTDD", "a"); + if (fp_append == NULL) + { + perror("fopen append failed"); + printf("errno = %d\n", __errno()); + return 1; + } - /* Close the file. */ - fclose(fp); + fprintf(fp_append, "Appended from C program!\n"); + fclose(fp_append); printf("Append complete.\n"); return 0; } - -