/*
ProgEx36 -- Using Win32 Api Functions For Random File Access Instead of
Standard C Library Functions.
In the program below are trees. I assign 14 of them to a TREE struct in
main(). Then we write them to a data file using a wrapper around WriteFile()
and read them back out with a wrapper around ReadFile(). We use CreateFile()
in the Win Api instead of the C runtime functions. Naturally, this won't work
too well in Linux!
*/
#include <Windows.h>
#include <stdio.h>
typedef struct TREE
{
short int sp; //Species code, e.g., 300=live Red oak, 310 live Black oak, etc
short int dbh; //Diameter at breast height (4.5 feet) in inches
short int ht; //Sawlog height in 16 foot logs times 10, i.e., 15=1.5 etc
short int cull; //% defect
short int grade;//Quality estimate
short int block;//Most of our sales contain multiple cutting blocks
short int bkctr;//each block starts its own unique tree count from 0
short int cruid;//control information
}Tree;
DWORD Put(HANDLE hFile, UINT iRecNum, LPCVOID lpRec, DWORD dwSize)
{
DWORD dwWritten;
UINT iRet=FALSE;
if(iRecNum)
{
if(SetFilePointer(hFile,dwSize*(iRecNum-1),NULL,FILE_BEGIN)!=0xFFFFFFFF)
{
iRet=WriteFile(hFile,lpRec,dwSize,&dwWritten,NULL);
if(iRet==FALSE||dwWritten!=dwSize)
return FALSE;
else
return TRUE;
}
else
return FALSE;
}
return FALSE;
}
DWORD Get(HANDLE hFile, UINT iRecNum, LPVOID lpRec, DWORD dwSize)
{
UINT iRet=FALSE;
DWORD dwRead;
if(iRecNum)
{
if(SetFilePointer(hFile,dwSize*(iRecNum-1),NULL,FILE_BEGIN)!=0xFFFFFFFF)
{
iRet=ReadFile(hFile,lpRec,dwSize,&dwRead,NULL);
if(iRet==FALSE||dwRead!=dwSize)
return FALSE;
else
return TRUE;
}
else
return FALSE;
}
return 1;
}
int main(void)
{
HANDLE hFile;
Tree t[]=
{
{300,12,10,0,3,1,1,0}, //
{310,14,15,0,2,1,2,0}, //
{320,16,20,0,2,1,3,0}, //
{400,18,25,0,1,1,4,0}, //
{480,20,30,0,1,1,5,0}, //
{210,22,35,0,2,1,6,0}, //
{590,24,40,0,1,1,7,0}, //
{300,12,15,0,3,1,8,0}, //
{310,14,20,0,2,1,9,0}, //
{320,16,25,0,2,1,10,0},//
{400,18,30,0,1,1,11,0},//
{480,20,35,0,1,1,12,0},//
{210,22,40,0,2,1,13,0},//
{590,24,45,0,1,1,14,0},//
};
Tree tree;
hFile=CreateFile("Data.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_FLAG_RANDOM_ACCESS,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
for(unsigned int i=1; i<=14; i++)
{
if(!Put(hFile,i,&t[i-1],sizeof(Tree)))
{
puts("File Write Error!");
exit(EXIT_FAILURE);
}
}
CloseHandle(hFile);
}
hFile=CreateFile("Data.dat",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_FLAG_RANDOM_ACCESS,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
printf("Tree #\tSpecies\tDbh\tLogs\t%%Cull\tGrade\tBlock\tBkCtr\n");
printf("=============================================================\n");
for(unsigned int i=1; i<=14; i++)
{
if(!Get(hFile,i,&tree,sizeof(Tree)))
{
puts("File Read Error!");
exit(EXIT_FAILURE);
}
printf
(
"%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\n",
i,tree.sp,tree.dbh,tree.ht,tree.cull,tree.grade,tree.block,tree.bkctr
);
}
CloseHandle(hFile);
}
getchar();
return EXIT_SUCCESS;
}
/*
Tree # Species Dbh Logs %Cull Grade Block BkCtr
=============================================================
1 300 12 10 0 3 1 1
2 310 14 15 0 2 1 2
3 320 16 20 0 2 1 3
4 400 18 25 0 1 1 4
5 480 20 30 0 1 1 5
6 210 22 35 0 2 1 6
7 590 24 40 0 1 1 7
8 300 12 15 0 3 1 8
9 310 14 20 0 2 1 9
10 320 16 25 0 2 1 10
11 400 18 30 0 1 1 11
12 480 20 35 0 1 1 12
13 210 22 40 0 2 1 13
14 590 24 45 0 1 1 14
*/
I AM DOING SOMETHING WRONG I CAN NOT GET IT TO RUN UNDER VS C++
THANKS FOR ANY HELP