George2 |
09-29-2006 08:25 AM |
Thank you for your quick sample, exvor!
Quote:
Originally Posted by exvor
I have acutally gotten my code to work properly now but im not sure its really what your trying to do. In my example it only works with a struct called datanode that gets passed to my read and write functions. Im not working with a list of stuff.
anyway here is my write function (it will be totaly dismantaled and rewritten so that It has more functionality.) in the new version it wont look for the correct place anymore but rather take a file postion from my list function and just write data using that file postion. hope this gets your ideas going tho.
Code:
#include<stdio.h>
#include<stdlib.h>
#include"cddat.h"
int addcd(DATANODE *infoadd,const char *database,int option)
{
FILE *cddat;
int number;
int loop = 0;
DATANODE *chek;
if ((cddat = fopen(database,"r+")) == NULL)
{
printf("\nError writeing to file!. func addcd\n");
return 1;
}
number = infoadd->cdnum;
if (option == 1)
{
fwrite(infoadd,sizeof(DATANODE),1,cddat);
}
else if (option == 2)
{
while(feof(cddat) == 0)
{
chek = malloc(DATANODE);
fseek(cddat,loop * sizeof(DATANODE),SEEK_SET);
fread(chek,sizeof(DATANODE),1,cddat);
if (chek->cdnum == number)
{
free(chek);
fwrite(infoadd,sizeof(DATANODE),1,cddat);
return 0;
}
else
{
loop++;
free(chek);
}
}
return 2; /* This only occurs if the element is not in the file */
}
else
{ fclose(cddat);
return 1;
}
if (fclose(cddat))
{
printf("\nError closeing file. func addcd\n");
return 1;
}
else
return 0;
}
as you can see checking for memory issues is not built in yet.
here is the lister by the way it acutally returns file pos now
Code:
#include<stdio.h>
#include<stdlib.h>
#include"cddat.h"
long int listfile(const char *basename)
{
FILE *basefile;
DATANODE *temphold;
int discnum;
int check;
int looper = 0;
if ((basefile = fopen(basename,"r")) != NULL)
{
temphold = malloc(sizeof(DATANODE));
printf("\nWhat disc would you like to access?\n");
printf("DISK#=");
scanf("%d",&discnum);
do {
fseek(basefile,looper * sizeof(DATANODE),SEEK_SET);
fread(temphold,sizeof(DATANODE),1,basefile);
check = temphold->cdnum;
printf("\ndiscnum %d, check %d\n",discnum,check);
if (discnum == check)
{
printf("\nRecord # %d\n",temphold->cdnum);
printf("+----------+\n");
printf("| DISC NAME| %s\n",temphold->cdname);
printf("+----------+\n");
printf("| TYPE | ");
switch(temphold->disktype)
{
case 1: printf("CD");
break;
case 2: printf("DVD");
break;
case 3: printf("Data CD");
break;
case 4: printf("Data DVD");
break;
default: printf("UNDEFINED");
break;
}
printf("\n+----------+\n");
printf("| CONDITION| ");
if (temphold->condition == 1)
printf("BAD");
else
printf("GOOD");
printf("\n+----------+\n");
printf("| LISTFILE | ");
printf("Note at this time listfiles are disabled");
printf("\n+----------+\n");
fclose(basefile);
free(temphold);
return (looper * sizeof(DATANODE));
}
looper++;
}while(feof(basefile) == 0);
printf("End of file reached record not found\n");
fclose(basefile);
free(temphold);
return 0;
}
fclose(basefile);
free(temphold);
return 1;
}
[
|
Could you help to review whether I have understood your points correctly in the sample. The addcd function will add a new node to the beginning of the database file if option is 1, and it will replace the exact node if option is 2, and listfile function will enumerate all the records in a file and print matched one.
My understanding correct?
regards,
George
|