Ive been trying to understand the below code for a while and need to know if it "acts like a record" for a single user. At the end users[userdb] is like an array for 6 struct user records?

#define userdb 5


/* DataBase*/
struct user
{char username[10];
char userid[9];
char password[4] ;
}users[userdb];

I need to have a simple database that stores 5 records that contain the name id and pass of 5 different users. And then write search or over write certain records and or a field within that record.

I have very little knowlege on how any of this type of stuff works but ive been researching and found a program that does pretty much what i want to do with my struct but it does it with a single variable the code is below. i placed sleep statements to see what it does.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<math.h>
#include<ctype.h>


/* random record description - could be anything */


struct rec


{


int x,y,z;


};


/* writes and then reads 10 arbitrary records from the file "junk". */


void main()


{


int i,j;


FILE *f;


struct rec r;


/* create the file of 10 records */


f=fopen("junk","w");


for (i=1;i<=10; i++)


{


r.x=i;


fwrite(&r,sizeof(struct rec),1,f);


}


fclose(f);


/* read the 10 records */


f=fopen("junk","r");


for (i=1;i<=10; i++)


{


fread(&r,sizeof(struct rec),1,f);


printf("%d\n",r.x);
sleep(4);


}


fclose(f);


printf("\n");
sleep(4);



/* use fseek to read the 10 records in reverse order */


f=fopen("junk","r");


for (i=9; i>=0; i--)


{


fseek(f,sizeof(struct rec)*i,SEEK_SET);


fread(&r,sizeof(struct rec),1,f);


printf("%d\n",r.x);
sleep(4);
}


fclose(f);


printf("\n");
sleep(4);



/* use fseek to read every other record */


f=fopen("junk","r");


fseek(f,0,SEEK_SET);


for (i=0;i<5; i++)


{


fread(&r,sizeof(struct rec),1,f);


printf("%d\n",r.x);
sleep(4);
fseek(f,sizeof(struct rec),SEEK_CUR);


}


fclose(f);


printf("\n");
sleep(4);



/* use fseek to read 4th record, change it, and write it back */


f=fopen("junk","r+");


fseek(f,sizeof(struct rec)*3,SEEK_SET);


fread(&r,sizeof(struct rec),1,f);


r.x=100;


fseek(f,sizeof(struct rec)*3,SEEK_SET);


fwrite(&r,sizeof(struct rec),1,f);


fclose(f);


printf("\n");
sleep(4);



/* read the 10 records to insure 4th record was changed */


f=fopen("junk","r");


for (i=1;i<=10; i++)


{


fread(&r,sizeof(struct rec),1,f);


printf("%d\n",r.x);
sleep(4);
}


fclose(f);


}

It only minipulates the x value in the struct writes reads and changes a value in that certain position. I need my struct to do the same thing but have know idea how to start.

I know I put alot in this post but i've been looking into this for a while and cant seem to get anyware.

> need to know if it "acts like a record" for a single user.
yes, it does. a struct is an aggregate that collects related pieces of information together in one place.

> At the end users[userdb] is like an array for 6 struct user records? #define userdb 5 userdb is a manifest constant equivalent to a literal 5 ). users[userdb] is an array of 5 user records. users[0], users[1], users[2], users[3], users[4] .

> I need to have a simple database that stores 5 records ...
> ... search or over write certain records and or a field within that record.
> ... found a program that does pretty much what i want to do
just a glance at the #include statements would tell you that it is not a program that you should be using as an example.
perhaps you could have a look at http://www.lysator.liu.se/c/bwk-tutor.html#structure
note: if you decide to peruse more of kernighan's C tutorial, scroll all the way to the top and read the disclaimer first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.