C - Error with read() of a file, storage in an array, and printing output properly
- by ns1
I am new to C, so I am not exactly sure where my error is. However, I do know that the great portion of the issue lies either in how I am storing the doubles in the d_buffer (double) array or the way I am printing it.
Specifically, my output keeps printing extremely large numbers (with around 10-12 digits before the decimal point and a trail of zeros after it. Additionally, this is an adaptation of an older program to allow for double inputs, so I only really added the two if statements (in the "read" for loop and the "printf" for loop) and the d_buffer declaration.
I would appreciate any input whatsoever as I have spent several hours on this error.
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
struct DataDescription
{
char fieldname[30];
char fieldtype;
int fieldsize;
};
/* -----------------------------------------------
eof(fd): returns 1 if file `fd' is out of data
----------------------------------------------- */
int eof(int fd)
{
char c;
if ( read(fd, &c, 1) != 1 )
return(1);
else
{ lseek(fd, -1, SEEK_CUR);
return(0);
}
}
void main()
{
FILE *fp; /* Used to access meta data */
int fd; /* Used to access user data */
/* ----------------------------------------------------------------
Variables to hold the description of the data - max 10 fields
---------------------------------------------------------------- */
struct DataDescription DataDes[10]; /* Holds data descriptions
for upto 10 fields */
int n_fields; /* Actual # fields */
/* ------------------------------------------------------
Variables to hold the data - max 10 fields....
------------------------------------------------------ */
char c_buffer[10][100]; /* For character data */
int i_buffer[10]; /* For integer data */
double d_buffer[10];
int i, j;
int found;
printf("Program for searching a mini database:\n");
/* =============================
Read in meta information
============================= */
fp = fopen("db-description", "r");
n_fields = 0;
while ( fscanf(fp, "%s %c %d", DataDes[n_fields].fieldname,
&DataDes[n_fields].fieldtype,
&DataDes[n_fields].fieldsize) > 0 )
n_fields++;
/* ---
Prints meta information
--- */
printf("\nThe database consists of these fields:\n");
for (i = 0; i < n_fields; i++)
printf("Index %d: Fieldname `%s',\ttype = %c,\tsize = %d\n",
i, DataDes[i].fieldname, DataDes[i].fieldtype,
DataDes[i].fieldsize);
printf("\n\n");
/* ---
Open database file
--- */
fd = open("db-data", O_RDONLY);
/* ---
Print content of the database file
--- */
printf("\nThe database content is:\n");
while ( ! eof(fd) )
{ /* ------------------
Read next record
------------------ */
for (j = 0; j < n_fields; j++)
{
if ( DataDes[j].fieldtype == 'I' )
read(fd, &i_buffer[j], DataDes[j].fieldsize);
if ( DataDes[j].fieldtype == 'F' )
read(fd, &d_buffer[j], DataDes[j].fieldsize);
if ( DataDes[j].fieldtype == 'C' )
read(fd, &c_buffer[j], DataDes[j].fieldsize);
}
double d;
/* ------------------
Print it...
------------------ */
for (j = 0; j < n_fields; j++)
{
if ( DataDes[j].fieldtype == 'I' )
printf("%d ", i_buffer[j]);
if ( DataDes[j].fieldtype == 'F' )
d = d_buffer[j];
printf("%lf ", d);
if ( DataDes[j].fieldtype == 'C' )
printf("%s ", c_buffer[j]);
}
printf("\n");
}
printf("\n");
printf("\n");
}
Post edits output:
16777216 0.000000 107245694331284094976.000000 107245694331284094976.000000 Pi
33554432 107245694331284094976.000000 2954938175610156848888276006519501238173891974277081114627768841840801736306392481516295906896346039950625609765296207682724801406770458881439696544971142710292689518104183685723154223544599940711614138798312668264956190761622328617992192.000000 2954938175610156848888276006519501238173891974277081114627768841840801736306392481516295906896346039950625609765296207682724801406770458881439696544971142710292689518104183685723154223544599940711614138798312668264956190761622328617992192.000000 Secret Key
50331648 2954938175610156848888276006519501238173891974277081114627768841840801736306392481516295906896346039950625609765296207682724801406770458881439696544971142710292689518104183685723154223544599940711614138798312668264956190761622328617992192.000000 -0.000000 -0.000000 The number E
Expected Output:
3 rows of data ending with the number "e = 2.18281828"
To reproduce the problem, the following two files need to be in the same directory as the lookup-data.c file:
- db-data
- db-description