OK here's the code for my structs:
#define num 999 // Para que sean todos las estructuras de 999
/* Estructuras principales */
struct direccion
{
char calle[15]; //Limitado a 15 characteres
int numportal; //1 digito
int piso; //1 digito
};
struct poblacion
{
char nombrepoblacion[15]; //Limitado a 15 characteres
int codpostal; //Limitado a 5 digitos
char provincia[15]; //Limitado a 15 characteres
};
struct empleados
{
int id; // Limitado a una letra E seguido de 3 numeros
char dni[11]; //Limitado a 8 numeros, un guion y una letra
char nombre[15]; //Limitado a 15 characteres
char apellido[15]; //Limitado a 15 characteres
int telefono; //Limitado a 9 digitos
struct direccion dir; // Una variable que es de tipo estructura dirrecion: acesso a calle,
//Numportal, y piso
struct poblacion pob; // Una variable que es de tipo estructura poblacion: accesso a
// nombrepoblacion, codpostal, y provincia
}e[num];
/* Asi podemos insertar hasta 999 productos, porque he creado
la tabla que contiene num elementos de tipo empleados: Este numero es el mismo que el id */
struct clientes
{
int id; // Limitado a una letra C seguido de 3 numeros
char dni[11]; //Limitado a 8 numeros, un espacio y una letra
char nombre[15];//Limitado a 15 characteres
char apellido[15];//Limitado a 15 characteres
int telefono;//Limitado a 6 digitos
struct direccion dir; // Una variable que es de tipo estructura dirrecion: acesso a calle,
//Numportal, y piso
struct poblacion pob; // Una variable que es de tipo estructura poblacion: accesso a
// nombrepoblacion, codpostal, y provincia
int cantcompras; // La cantidad de comprars para los descuentos
}c[num];
/* Asi podemos insertar hasta 999 productos, porque he creado
la tabla que contiene num elementos de tipo cliente: Este numero es el mismo que el id */
struct productos
{
int id; // Limitado a una letra P seguido de 3 numeros
char nombre[15]; //Limitado a 15 characteres
char descripcion[50]; //Limitado a 50 characteres
float precio; // Precio con decimal
int cantidad; //Lo que queda en stock
}p[num];
/* Asi podemos insertar hasta 999 productos, porque he creado
la tabla que contiene num elementos de tipo producto: Este numero es el mismo que el id */
Ill explain:
p is a variable that I can use to access each field. It is a array so I can access all 999 possible clients in the struct (why? explanation below
void inicializar ()
{ // Principio de funciones inicizalizar
int a=0; // 1 Contador
/********************* Inicializamos la estructura de clientes *************************/
/* Campos int id, char dni, char nombre, char apellido, int telefono */
for (a=0;a<999;a++)
{
c[a].id=0;
strcpy(c[a].dni,"0");
strcpy(c[a].nombre,"0");
strcpy(c[a].apellido,"0");
c[a].telefono=0;
c[a].cantcompras=0;
strcpy(c[a].dir.calle,"0");
c[a].dir.numportal=0;
c[a].dir.piso=0;
c[a].pob.codpostal=0;
strcpy(c[a].pob.nombrepoblacion,"0");
strcpy(c[a].pob.provincia,"0");
}
/***************** Fin de inicializacion de clientes ******************************/
/********************* Inicializamos la estructura de empleados *************************/
/* Campos: int id, char dni, char nombre, char apellido, int telefono */
for (a=0;a<999;a++)
{
e[a].id=0;
strcpy(e[a].dni,"0");
strcpy(e[a].nombre,"0");
strcpy(e[a].apellido,"0");
e[a].telefono=0;
strcpy(e[a].dir.calle,"0");
e[a].dir.numportal=0;
e[a].dir.piso=0;
e[a].pob.codpostal=0;
strcpy(e[a].pob.nombrepoblacion,"0");
strcpy(e[a].pob.provincia,"0");
}
/********************* Fin de inicializacion de empleados ******************************/
/***************** Inicializamos la estructura de productos ***********************/
for (a=0;a<999;a++) // Este for permite que cada 999 de los productos lo ponga a 0
{
p[a].id=0;
strcpy(p[a].nombre,"0");
strcpy(p[a].descripcion,"0");
p[a].precio=0.0;
p[a].cantidad=0;
}
/***************** Fin de inicializacion de productos ******************************/
} // Fin de funciones de inicializar
As you can see I set here all values to 0. Why? Because I use 0 or "0" as a wildcard for some function to see if the client's empty or not.
The point is that I have to do all of this (add,modify,delete,etc) using a pointer that acceses a file instead of setting it values. In this file, I should have all of the fields and everytime I load the program it sets the values in the file to the struct. I work locally with the struct and when I end the program it saves all of it to a file. I have this so far:
void inicializarporarchivo()
{
FILE *ficheroc;
FILE *ficheroe;
FILE *ficherop;
int i;
char tid[1];
char ttelefono[9];
char tcantcompras[1];
char tnumportal[2];
char tpiso[1];
ficheroc=fopen("clientes.txt","r");
ficheroe=fopen("empleados.txt","r");
ficherop=fopen("productos.txt","r");
if (ficheroc==NULL)
{
void inicializar();
}
/* fclose(ficheroc);
ficheroc=fopen("clientes.txt","w");
for (i=0;i<999;i++)
{
fputs("Cliente ",ficheroc);
fputc(i,ficheroc);
fputs(":",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputc("0",ficheroc);
fputs("\n",ficheroc);
}*/
if (ficheroc!=NULL)
{
do
{
for (i=0;i<999;i++)
{
fgets(tid,sizeof(tid),ficheroc);
c[i].id=atoi(tid);
fgets(c[i].dni,sizeof(c[i].dni),ficheroc);
fgets(c[i].nombre,sizeof(c[i].nombre),ficheroc);
fgets(c[i].apellido,sizeof(c[i].apellido),ficheroc);
fgets(ttelefono,sizeof(ttelefono),ficheroc);
c[i].telefono=atoi(ttelefono);
fgets(tcantcompras,sizeof(tcantcompras),ficheroc);
c[i].cantcompras=atoi(tcantcompras);
fgets(c[i].dir.calle,sizeof(c[i].dir.calle),ficheroc);
fgets(tnumportal,sizeof(tnumportal),ficheroc);
c[i].dir.numportal=atoi(tnumportal);
fgets(tpiso,sizeof(tpiso),ficheroc);
c[i].dir.piso=atoi(tpiso);
fgets(c[i].dir.calle,sizeof(c[i].dir.calle),ficheroc);
fgets(c[i].pob.nombrepoblacion,sizeof(c[i].pob.nombrepoblacion),ficheroc);
fgets(c[i].pob.provincia,sizeof(c[i].pob.provincia),ficheroc);
printf ("%s",c[i].dni);
getch();
}
putc("\0",ficheroc);
}while (feof(ficheroc)!=1);
}
}
But it doesnt work nor does it use a pointer.
Objectives:
1: Use a pointer to access the data in each field.
2: Load/save from/to a file.