Using fread to read data from a database!! 
Author Message
 Using fread to read data from a database!!

The course I have been doing has covered reading data from text and binary
files using fscanf, fgets, fread etc.. These all read data until a space, tab,
newline or EOF is reached but if the file I want to read from is a database do
I use the same format i.e.

fread(&order, sizeof(order), 1 , test_ptr);

*where test_ptr is a pointer to a database file - in this case a microsoft
excel file.

Thanks

Patricia B.



Fri, 14 Nov 2003 21:24:08 GMT  
 Using fread to read data from a database!!

Quote:
> The course I have been doing has covered reading data from text
> and binary files using fscanf, fgets, fread etc.. These all
> read data until a space, tab, newline or EOF is reached but if
> the file I want to read from is a database do I use the same
> format i.e.

> fread(&order, sizeof(order), 1 , test_ptr);

well, fread() doesn't read until some special character occurs,
but allways tries to read the given number of bytes from the
file. So if order is an int and sizeof(int) is 4, you'll get 4
bytes written into order. On a DOS system you may need to
specify that your file is binary at the fopen() call.

If fread returns a number different from that passed in its
seconds arg, you may want to check for errors or eof on the
file (ferror() and feof()).

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Fri, 14 Nov 2003 21:49:02 GMT  
 Using fread to read data from a database!!

Quote:
>The course I have been doing has covered reading data from text and binary
>files using fscanf, fgets, fread etc.. These all read data until a space, tab,
>newline or EOF is reached but if the file I want to read from is a database do
>I use the same format i.e.

>fread(&order, sizeof(order), 1 , test_ptr);

>*where test_ptr is a pointer to a database file - in this case a microsoft
>excel file.

Patricia, the dBASE type files have a header that is of variable
length and depends on the structure.  You need to read in the fixed
portion (as you do above):

#typedef unsigned long ulong
#typedef unsigned short ushort

struct TABLE_HEADER {
    char    type;
    char    last_update[3];
    ulong   records;
    ushort  first_record;
    ushort  record_length;
    char    reserved1[16];
    char    flags;
    char    code_page;
    char    reserved2[2];

Quote:
};

And then you read in this structure one time for every field in the
table:

struct FIELD_RECORD {
    char    name[10];
    char    type;
    ulong   offset;
    char    length;
    char    decimals;
    char    flags;
    char    reserved[13];

Quote:
};

Note the "ushort first_record" in the table_header.  This indicates
where the actual data starts.  You'll have to also note that each
record contains a deleted() mark and that will exist at offset 0.  So,
in order to get to a particular field offset you'll have to assume
base-1 instead of base-0.

What I do in my code is something like this:

FILE *fh;
struct TABLE_HEADER th;
struct FIELD_RECORD *fptr;
char *buf;
int numread;

fh = fopen("whatever.dbf","rb+");
numread = fread(th, sizeof(th), 1, fh);
if (numread != sizeof(th)) return -1;
buf = malloc(th.first_record - sizeof(th));
if (buf == null) return -1;
numread = fread(buf, th.first_record - sizeof(th), 1, fh);
if (numread != th.first_record - sizeof(th)) return -1;
// Right now, the file is positioned to the first record
// You have read in the table header
// And you have the structure in buf that can be accessed
// using "fptr = (struct FIELD_RECORD *)buf;"

If you have any additional questions, please email me.

- Rick C. Hodgin



Fri, 14 Nov 2003 22:28:45 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. why does this fread fail, trying to read 100,000 rec in one fread

2. Read a file using fread trouble

3. Seek version of fread which uses read

4. help needed fread'ing data into a buffer using Turbo C++ and huge model

5. : Reading full screen images from file using fread()?

6. Read Data from Paradox database files?

7. C sources for reading/writing binary data to an Access database

8. C sources for reading/writing binary data to an Access database

9. Reading data from a database (.db) file

10. read-only database using ODBC to update dbase 5

11. Reading/writing to an Access 2000 database through the Intenet using WIN32 app

12. Using DAO to read bitmaps from ACCESS database.

 

 
Powered by phpBB® Forum Software