Question: C code: I need to create a program using those functions and arguments mentioned bellow. 3.3 Reading Binary Integers In read_deltas.c, define the following function.
C code: I need to create a program using those functions and arguments mentioned bellow.
3.3 Reading Binary Integers
In read_deltas.c, define the following function.
int *read_int_deltas(char *fname, int *len); // Reads integers in binary delta format from the file named by fname // and returns an array of them. The first integer in the file gives // the starting point and subsequent integers are changes from the // previous total. // // Integers in the file are in binary format so the size of the file // in bytes indicates the quantity of integers. Uses the stat() system // call to determine the file size in bytes which then allows an array // of appropriate size to be allocated. DOES NOT scan through the file // to count its size as this is not needed. // // Opens the file with fopen() and uses repeated calls to fread() to // read binary integers into the allocated array. Does delta // computations as integers are read. Closes the file after reading // all ints. // // The argument len is a pointer to an integer which is set to // the length of the array that is allocated by the function. // // If the file cannot be opened with fopen() or file is smaller than // the size of 1 int, sets len to -1 and returns NULL.
Binary Delta Format
The file format expected by this file is binary rather than textual so it is likely not possible to examine the data in text format. The data/ directory contains some example files
- data/short.int : 84 bytes : 21 integers
- data/wave.int : 508 bytes : 127 integers
The data in these are identical to the .txt equivalents:
- The first number is the starting point
- Subsequent numbers are changes of the previous number
However, rather than use ASCII characters that are human readable, each integer is stored directly as 4 bytes in the file.
Determining File Size / Array Elements
Since each integer occupies 4 bytes directly in the file, the size of the file corresponds to the number of integers in the file. This allows one to allocate an appropriately sized array based on the file size. This means that no scanning is required to determine how many ints are in the file.
The stat() system call in Unix provides information about a file by filling in values of a struct. The file size in bytes is one such piece of information. Use a calling sequence like the following to determine the binary file size.
struct stat sb; // struct to hold int result = stat(fname, &sb); // unix system call to determine size of named file if(result==-1 || sb.st_size < sizeof(int)){ // if something went wrong or bail if file is too small return NULL; } int total_bytes = sb.st_size; // size of file in bytes Note the use of the struct stat sb, on old convention for declaring a struct as a stack variable. Its memory address is passed to stat() which fills in information. The field st_size is the number of bytes in the file. This can then be used to determine how many ints are in the file (4 bytes per int) and allocate an array with malloc() prior to reading through the file.
Note that this technique only works with binary files. Text files use a variable number of characters/bytes per integer and include formatting characters such as spaces and newlines. This means the number of bytes in the file does not correspond to the number of integers in it.
Reading Binary Data
Files with binary data can be opened identically to the text data but reading requires use of the fread() function. This function differs greatly from fscanf() as it has no format string. Instead, it is meant to read raw bits/bytes from a file into a specified memory address. It is likely that you will use a call similar to the following.
fread(&values[i], sizeof(int), 1, fin); Address to | bytes for |quantity |file handle store data | one element |to read |to read from
This call reads a single binary integer into the an array element from an open file handle.
Make sure as you read integers into the array you perform the delta computation of adding on the previous value to the recently read delta.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
