break read and append into two operations
This commit is contained in:
parent
78ea32b3d9
commit
5742a21d59
@ -1,5 +1,5 @@
|
|||||||
//TXTWKR01 JOB (GCC),'File I/O Example',
|
//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
|
// MSGLEVEL=(1,1),REGION=4M,TIME=1440
|
||||||
//* Compile and Go Step
|
//* Compile and Go Step
|
||||||
//STEP1 EXEC GCCCG,INFILE='@05054.SRCLIB.C(TXTWKR01)'
|
//STEP1 EXEC GCCCG,INFILE='@05054.SRCLIB.C(TXTWKR01)'
|
||||||
|
|||||||
@ -10,35 +10,39 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp_read;
|
||||||
|
FILE *fp_append;
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
/* Open the file in update mode (read and write). */
|
/* First: open for reading only to dump current content */
|
||||||
/* The filename is "dd:DDNAME" where DDNAME is defined in JCL. */
|
fp_read = fopen("dd:TESTDD", "r");
|
||||||
fp = fopen("dd:TESTDD", "a+");
|
if (fp_read == NULL)
|
||||||
if (fp == NULL)
|
|
||||||
{
|
{
|
||||||
perror("fopen failed.");
|
perror("fopen read failed");
|
||||||
printf("errno = %d\n", __errno());
|
printf("errno = %d\n", __errno());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read and print existing lines (to SYSOUT). */
|
|
||||||
printf("Existing content:\n");
|
printf("Existing content:\n");
|
||||||
while (fgets(buffer, BUFFER_SIZE, fp) != NULL)
|
while (fgets(buffer, BUFFER_SIZE, fp_read) != NULL)
|
||||||
{
|
{
|
||||||
printf("%s", buffer);
|
printf("%s", buffer);
|
||||||
}
|
}
|
||||||
|
fclose(fp_read);
|
||||||
|
|
||||||
/* After reading to EOF, the file position is at the end, so we can append. */
|
/* Second: open for append only to add the new line */
|
||||||
fprintf(fp, "Appended from C program!\n");
|
fp_append = fopen("dd:TESTDD", "a");
|
||||||
|
if (fp_append == NULL)
|
||||||
|
{
|
||||||
|
perror("fopen append failed");
|
||||||
|
printf("errno = %d\n", __errno());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Close the file. */
|
fprintf(fp_append, "Appended from C program!\n");
|
||||||
fclose(fp);
|
fclose(fp_append);
|
||||||
|
|
||||||
printf("Append complete.\n");
|
printf("Append complete.\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user