ILE C/C++ Programmer's Guide

Updating Binary Stream Files (character at a time)

If the amount of data being updated exceeds the current record length, then the excess data updates the next record. If the current record is the last record in the file, a new record is created.

Figure 94. Updating a Binary Stream File with Data Longer than Record Length



Example:

The following example illustrates updating a binary stream file with data that is longer than the record length.

Figure 95. ILE C Source to Update a Binary Stream File with Data Longer than the Record Length




#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char buf[5] = "12345";
/* Open an existing binary file for updating. */
if (( fp = fopen ( "QTEMP/TEST(MBR)", "rb+" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
/* Write 5 characters from the buffer to the file. */

fwrite ( buf, 1, sizeof(buf), fp );

fclose ( fp );
return 0;
}

If the amount of data being updated is shorter than the current record length, then the record is partially updated and the remainder is unchanged.

Figure 96. Updating a Binary Stream File with Data Shorter than Record Length


Example:

The following example illustrates updating a binary stream file with data that is shorter than the record length.

Figure 97. ILE C Source to Update a Binary Stream File with Data Shorter than the Record Length




#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char buf[2] = "12";
/* Open an existing binary file for updating. */
if (( fp = fopen ( "QTEMP/TEST(MBR)", "rb+" ) ) == NULL )
{
printf ( "Cannot open file\n" );
exit ( 1 );
}
/* Write 2 characters from the buffer to the file. */

fwrite ( buf, 1, sizeof(buf), fp );

fclose ( fp );
}


[ Top of Page | Previous Page | Next Page | Table of Contents ]