tf_read_record(), sf_read_record()

Purpose

Reads a DP4 record from a file

Old names

#define _vread tf_read_record
#define _aread sf_read_record

Syntax (CCOWN)

void tf_read_record(int file_handle, short *datarec)
void sf_read_record(int file_handle, short * datarec);

Syntax (DP4DBAPI)

BOOLEAN dp4_sf_read_record(FCONN * fconn,short * datarec)

Parameters

int file_handle

File handle of file

 

FCONN * fconn

File handle of file

 

short * datarec

Pointer to a DP4 record to hold record read from file

Description

The tf_read_record() and sf_read_record() functions read a DP4 record from the current position in the file. You obtain the file handle with a successful call to tf_open() or sf_open().

The sf_read_record() function calls the file handling support in the database manager, and the tf_read_record() function calls the file handling support in the terminal manager. You must not call an sf_ function with a handle obtained with tf_open(), or a tf_ function with a handle obtained with sf_open().

A DP4 record is a record corresponding to a structure of the form below:

struct MY_RECORD
{
short l;
/* and normally but not necessary */
short typenr;
short generation;
short flags;
long timestamp;
/* other fields */
...
}

The L field is used to determine the number of bytes to read from the file. The file pointer is automatically incremented.

datarec must be large enough to hold the largest expected record.

Do not mix calls to the function tf_read_record() with calls to the function tf_write(). This is because the function tf_read_record() allows for any byte ordering differences on non-INTEL machines, but the function tf_write() does no such translation.

This function is not intended for reading database files directly, but only for reading and writing extract files, or similar files you have created yourself.

See also

tf_read(), tf_write_record(), tf_open()

Example

/*
C Functions Reference Manual
----------------------------
Filename: TF_READ_.C
Example: tf_read_record, and various other tf_XXXX
functions
Purpose: Simple import program which reads and
posts records from a file. The example for
tf_write_record generates a file which can
be used here.
*/

/*
#db salesord
#c
#end
*/

#include "dp4.h"
#include "tf_read_.h"
/* generate this file by running LIBMAKE */

/* prototypes */
static int get_valid_file(char*);

/* variables */
static struct DATAREC datarec;

/* general DP4 record */
void process()
{
char fname[13]; /* file name holder */
map_draw(1,RETAIN); /* header map */
WIPE(fname); /* blank file_name buffer */
if (get_valid_file(fname) == TRUE)
/* get name of existing file from user */
{
int fd;
file_pointer size;
fd = tf_open(fname, VF_RO|VF_SEQ);
/* open file read-only, sequential */
if (fd 0)
{
size = tf_get_size(fd);
/* get size of user specified file */
if (size 0)
{
int count;
map_draw(2,RETAIN);
/* HEADER_SIZE is a minimum record
length */
for (count = 0;((size -
tf_get_position(fd))HEADER_SIZE);
count++)
{
tf_read_record(fd,&datarec.l);
rec_post_with_flags(&datarec.l,
NO_CHECK);
show_h(2,1,count);
trm_refresh();
/* make count visible */
if ((count % 20) == 0) db_commit();
/* commit every 20 records */
}
tf_close(fd); /* close the file */
}
else
askf_special(3,1,0);
/* tell user that file is empty */
}
else
askf_special(4,1,0);
/* tell user that open failed */
}
}

/**/

static get_valid_file(fname)
char* fname;
{
int valid_fname = FALSE;
/* not valid by default */
map_draw(10,RETAIN);
/* draw map to enter filename */
while (valid_fname == FALSE)
{
if (!askf_c(10,1,fname))
{
register i; /* strip out spaces */
for (i=0; i < 12 && fname[i] != ' '; i++);
fname[i] = '\0'; /* terminate string */
if (strlen(fname) > 0)
{
if (tf_check(fname) != 1)
/* 1 means file exists */
{
map_draw(11,RETAIN);
/* tell user file does not exist */
show_c(11,1,fname);
askf_special(11,2,0);
/* get user to continue */
}
else
{
valid_fname = TRUE;
/* found an existing file */
break;
}
}
}
else break;
}
return valid_fname;
}