Tuesday 28 October 2014

A database that uses a random access file written in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<stdbool.h>
struct contactinfo{
char name[40];
char number[40];
char email[40];
};

struct filedetails{
int filelength;
int recnum;
};
struct filedetails filespec;
struct contactinfo person;
struct contactinfo blankfields;
FILE *details;
int flag = 0;
char contact[40];
long int offset=0, length;
long int size;
char filename[30];
bool recordfound;
void newsubs(void), search(void), readLine(char buffer[]), new(void), sort(void);
void list(void), delete(void), prntxt(void);
int main(void)
{
int choice;
char ch[3];
for(;;){
printf("\n");
printf("1. New file\n");
printf("2. New record\n");
printf("3. Sort\n");
printf("4. List\n");
printf("5. Search\n");
printf("6. Delete\n");
printf("7. Export to a txt file for printing.\n");
printf("8. Quit:\n");
do{
printf("\nChoose...");
readLine(ch);
choice = atoi(ch);
if(choice==1)new();
if(choice==2)newsubs();
if(choice==3)sort();
if(choice==4)list();
if(choice==5)search();
if(choice==6)delete();
if(choice==7) prntxt();
if(choice==8) exit(0);
}while(choice < 1 && choice > 8);
}
return 0;
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}

void newsubs()
{
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL)
{
printf("Cannot open file.\n");
}
if((details=fopen(filename, "r+b"))!=NULL) {

fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Records in file =%d\n",filespec.recnum);
printf("Size of file in bytes = %d\n",filespec.filelength);
printf("Enter information on new contact.\n");
printf("Name:\n");
readLine(person.name);
printf("Phone number:\n");
readLine(person.number);
printf("Email:\n");
readLine(person.email);
size = sizeof(struct contactinfo);
offset=(filespec.recnum +1)*size;
if(fseek(details, offset,0))
printf("Seek error.\n");
length = ftell(details);
printf("Pointer before writing at %ld\n",length);
if(fwrite(&person, sizeof(struct contactinfo), 1,details)==0) {
printf("Write error.");
}
else
{
length = ftell(details);
printf("Pointer after writing at %ld\n",length);
filespec.filelength= length;
filespec.recnum+=1;
rewind(details);
fwrite(&filespec, sizeof(struct filedetails), 1,details);
person = blankfields;
fclose(details);
}
}
}

void search()
{
int searchcount=0;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL)
flag = 0;
recordfound=0;
fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Enter a name to look for.\n");

readLine(contact);
offset=0;
while(!feof(details))
{
searchcount+=1;
size = sizeof(struct contactinfo);
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);

if(strcmp(contact, person.name)==0)
{
recordfound=1;
printf("%s\n",person.name);
printf("%s\n",person.number);
printf("%s\n",person.email);
printf("\n");
}
if(searchcount == filespec.recnum)break;
}
}
fclose(details);
if(recordfound == 0)
{
printf("Record not found.\n");
}
else
{
if(recordfound == 1) recordfound== 0;
flag = 1;
}
}
}


void new()
{
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "w+b"))==NULL) {
printf("Cannot open file.\n");
}
filespec.recnum=0;
fwrite(&filespec , sizeof(struct filedetails), 1,details);
printf("New file created\n");
fclose(details);
}


void sort()
{
int recnum;
int i;
struct contactinfo temp;
long int u;
long int v;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
fread(&filespec , sizeof(struct filedetails), 1,details);
for(u=1; u<filespec.recnum; u++)
{
for (v=u+1; v<=filespec.recnum; v++){
size = sizeof(person);
fseek(details, sizeof(person)*u,0);
fread(&person, sizeof(person), 1, details);
fseek(details, sizeof(person)*(v),0);
fread(&temp, sizeof(person), 1, details);
if(strcmp(person.name,temp.name)>0)
{
fseek(details, sizeof(person)*(u),0);
fwrite(&temp, sizeof temp, 1,details);
fseek(details, sizeof(person)*(v),0);
fwrite(&person, sizeof(person), 1,details);
}
}
}
fclose(details);
}
}

void list()
{
int counter = 0;
printf("Enter a file name.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
printf("Records in file =%d\n",filespec.recnum);
printf("Size of file in bytes =%d\n", filespec.filelength);
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
counter +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
printf("\n");
printf("%s\n",person.name);
printf("%s\n",person.number);
printf("%s\n",person.email);
printf("\n");
if(counter == filespec.recnum)break;
}
}
fclose(details);
}
}   
   
void delete()
{   

long int offset2=0;
int recordcount;
int counter=0;
int counter2=0;
char filename2[30];
char reply;
FILE *tempfile;
search();
if(flag == 0)
{
printf("Exiting function delete.\n");
}
else
{
if((details=fopen(filename, "r+b"))==NULL)
{
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
recordcount = filespec.recnum;
printf("Enter a file name for the destination of the edited files.\n");
readLine(filename2);
if((tempfile=fopen(filename2, "w+b"))==NULL)
{
printf("Cannot open file.\n");
}
else
{
filespec.recnum=0;
fwrite(&filespec , sizeof(struct filedetails), 1,tempfile);
printf("New file created\n");
fclose(tempfile);
if((tempfile=fopen(filename2, "r+b"))!=NULL)
{
printf("File %s is open for input.\n",filename2);
}
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
person = blankfields;
counter +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
printf("%s\n",person.name);
if(strcmp(contact, person.name)!=0)
{
counter2+=1;
if(counter2 == recordcount) break;
offset2=(counter2)*size;
if(fseek(tempfile, offset2,0)){
printf("Seek error.");
}
else
{
if(fwrite(&person, sizeof(struct contactinfo), 1, tempfile));
length = ftell(tempfile);
}
}
}
}
fclose(details);
filespec.recnum=counter2-1;


filespec.filelength=length;
rewind(tempfile);
fwrite(&filespec , sizeof(struct filedetails), 1,tempfile);
fclose(tempfile);
}
}
}
}
}

void prntxt()
{
int count;
FILE *stream;
char filenametxt[30];
count = 0;
printf("Enter a 'filename.txt' file name to create and write to.\n");
readLine(filenametxt);
printf("\n");
stream = fopen(filenametxt, "w+");
if((details=fopen(filenametxt, "w+"))==NULL) {
printf("Cannot open file.\n");
}
else
{
int counter = 0;
printf("Enter a file name to read records from.\n");
readLine(filename);
printf("\n");
if((details=fopen(filename, "r+b"))==NULL) {
printf("Cannot open file.\n");
}
else
{
if((details=fopen(filename, "r+b"))!=NULL) {
fread(&filespec , sizeof(struct filedetails), 1,details);
size = sizeof(struct contactinfo);
offset=0;
while(!feof(details))
{
counter +=1;
count +=1;
offset+=size;
if(fseek(details, offset,0)){
printf("Seek error.");
}
else
{
fread(&person, sizeof(struct contactinfo), 1, details);
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", person.name);
fprintf(stream,"%s\n", person.number);
fprintf(stream,"%s\n", person.email);
fprintf(stream,"\n");
if(counter == filespec.recnum)break;
}
}
}
}
}
fclose(details);
fclose(stream);
}

No comments:

Post a Comment