Soluciones A Los Ejemplos de Ficheros
Soluciones A Los Ejemplos de Ficheros
Soluciones A Los Ejemplos de Ficheros
/* Programa: Crear_fichero.c */
#include <stdio.h>
#define EOL 10
main(argc, argv)
int argc;
char *argv[];
{
char nombre_fichero[20];
FILE * fd;
int c;
int ant;
if (argc != 2) {
printf ("\nIntroduzca nombre de fichero: ");
gets(nombre_fichero);
} else strcpy (nombre_fichero, argv[1]);
if ((fd = fopen (nombre_fichero, "w")) == NULL)
{ printf ("Error"); exit(); }
ant = EOL;
while ((c = getchar()) != '.' || ant != EOL) {
ant = c;
fputc (c, fd);
};
fclose (fd);
}
num = 0;
while (fgets(linea, 80, fd) != NULL) {
/* En linea esta toda la linea. Hay que extraer palabras */
while (extraer (palabra, &linea))
if (strcmp (palabra, cadena) == 0) num++;
};
printf ("\nNumero total de ocurrencias: %d\n", num);
}
/* Extrae un palabra en p, del puntero a vector s. */
/* char **s es un puntero a un vector. *s es el vector */
extraer (p, s)
char *p, **s;
{
char *tmp;
tmp = p;
while (valido (**s) == 0 && **s != '\0')
*s = *s + 1;
while (valido(**s) == 1){
*tmp++ = **s;
*s = *s + 1;
};
*tmp = '\0';
/* puts(p); */
return strlen(p);
}
valido(c)
char c;
{
return (isalpha(c) || isdigit(c));
}
nr = 0;
fich = fopen (fichero, "r");
while (lee_registro(&r, fich, nr++) != EOF){
printf ("\nRegistro: \n");
printf ("%20s ; %30s ; %8ld", r.nombre, r.apellidos, r.dni);
};
fclose(fich);
}
void lee_datos(r)
registro * r;
{
char ocho[9];
printf ("Dame el nombre: "); scanf(" \n%20s",r->nombre);
printf ("Dame los apellidos: "); scanf (" \n%30s", r->apellidos);
printf ("Dame el dni: "); scanf(" \n%s", ocho);
r->dni = atol(ocho);
}
int lee_registro (r, f, offset)
registro *r;
FILE * f;
int offset;
{
lseek (f, (long) offset * sizeof (registro), 0);
return fscanf (f, " \n%20s \n%30s \n%ld", r->nombre, r>apellidos, &(r->dni));
}
void escribe_registro (r, f)
registro r;
FILE *f;
{