
Lectura de archivos de texto en C
C/Visual C
Publicado el 26 de Febrero del 2002 por Alexis Leung (4 códigos)
16.732 visualizaciones desde el 26 de Febrero del 2002
Lectura de archivos de texto en C. Los presenta como un editor de MS-DOS.
/* AUTOR: ALEXIS LEUNG */
/* FECHA DE ULTIMA ACTUALIZACION: 25 - 02 - 02 */
/* E-MAIL: ARMA_ESMERALDA@HOTMAIL.COM */
/* NOTAS: EL PROGRAMA TAL VEZ TENGA PROBLEMAS AL LEER FICHEROS MUY LARGOS */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define KB_ESC 27
#define KB_UP 72
#define KB_DOWN 80
#define KB_REPAG 73
#define KB_AVPAG 81
typedef struct info {
char cad[80];
struct info *sig;
struct info *ant;
};
/*--------------------------VARIABLES GLOBALES----------------------------*/
struct info *inicio,*final;
int tlineas = 0; /* cantidad de l¡neas en el fichero */
int tam; /* tama¤o de la estructura */
/* -----------------------PROTOTIPO DE LAS FUNCIONES----------------------*/
int lee(char *nombre_f);
void muestra_fichero(void);
void libera(void);
void muestra_abajo(int linea_actual);
void muestra_arriba(void);
int main()
{
char *nombre_f = malloc (50);
tam = sizeof(struct info);
inicio = final = NULL;
clrscr();
printf("\n Introduzca el nombre y ruta completa del archivo: ");
gets(nombre_f);
if ( !lee(nombre_f) )
{
printf("\n ERROR: No se pudo abrir el archivo especificado");
return -1;
}
_setcursortype(_NOCURSOR);
muestra_fichero();
// libera();
clrscr();
textcolor(LIGHTGRAY);
return 0;
}
/* -----------------CREA LA LISTA DOBLEMENTE ENLAZADA---------------------*/
int lee(char *nombre_f)
{
FILE *f;
register int i;
struct info *aux;
f = fopen(nombre_f,"r");
if (!f)
return (0);
else
{
while (!feof(f))
{
if (!(aux = (struct info *) malloc( sizeof(struct info) ) ) )
{
printf("\n Memoria insuficiente ");
exit(0);
}
fgets(aux->cad,80,f);
if (!inicio) /* si es la primera l¡nea */
inicio = aux;
else
{
final->sig = aux;
aux->ant = final;
}
final = aux;
tlineas++;
}
inicio->ant = NULL;
final->sig = NULL;
aux = inicio;
}
fclose(f); /* cierra el fichero */
return (-1);
}
/* ------------------CUERPO PRINCIPAL DEL PROGRAMA------------------------*/
void muestra_fichero(void)
{
struct info *tope,*fin,*aux;
register int i; /* contador para bucles */
register char op; /* caracter de usuario */
register char cuenta = 0; /* bandera de tecla */
register int linea_actual = 0; /* linea actual */
aux = fin = tope = inicio; /* iniciamos los valores para que apunte inicio */
for (i = 1; i <= 23; i++)
{
fin = fin->sig;
linea_actual++;
if (fin == final)
break;
}
fin = fin->ant; /* retrocedemos para apuntar al puntero anterior */
textcolor(WHITE);
muestra_arriba();
while (op != KB_ESC)
{
if (!cuenta)
{
for (i = 1; i <= 23; i++)
{
gotoxy(1,i+1);
clreol();
cprintf("%s",aux->cad);
aux = aux->sig;
}
muestra_abajo(linea_actual);
}
for (;;)
{
op = getch(); /* espera el comando del teclado */
if (op != KB_UP && op != KB_DOWN && op != KB_ESC && op != KB_REPAG && op != KB_AVPAG)
continue;
break;
}
switch (op) {
/* si presiona arriba */
case KB_UP: {
if (tope == inicio)
{
cuenta = 1;
break;
}
linea_actual--;
cuenta = 0;
tope = tope->ant;
fin = fin->ant;
break;
}
case KB_DOWN: {
if (fin == final)
{
cuenta = 1;
break;
}
linea_actual++;
cuenta = 0;
tope = tope->sig;
fin = fin->sig;
break;
}
case KB_REPAG: {
if (tope == inicio)
{
cuenta = 1;
break;
}
cuenta = 0;
for (i = 1; i <= 23; i++)
{
tope = tope->ant;
fin = fin->ant;
if (tope == inicio)
{
linea_actual--;
break;
}
linea_actual--;
}
break;
}
case KB_AVPAG: {
if (fin == final)
{
cuenta = 1;
break;
}
cuenta = 0;
for (i = 1; i <= 23; i++)
{
tope = tope->sig;
fin = fin->sig;
if (fin == final)
{
linea_actual++;
break;
}
linea_actual++;
}
break;
}
} /* termina switch (op) */
aux = tope;
} /* termina while */
} /* fin de la funci¢n */
/*--------------------MUESTRA INFORMACION GENERAL-------------------------*/
void muestra_arriba(void)
{
gotoxy(1,1);
textbackground(GREEN);
cprintf("%65s%15s","LECTOR DE FICHEROS DE TEXTO 1.0 POR: ALEXIS LEUNG"," ");
textbackground(BLACK);
}
/*--------------------MUESTRA LA CANTIDAD DE LINEAS ----------------------*/
void muestra_abajo(int linea_actual)
{
gotoxy(1,25);
textbackground(GREEN);
cprintf(" Presione las flechas, RePag, AvPag para desplazarse%18s%4d/%4d","Linea:",linea_actual,tlineas);
textbackground(BLACK);
}
/*-----------------------LIBERA LA MEMORIA OCUPADA------------------------*/
void libera(void)
{
register int i;
struct info *aux,*aux2;
for (aux = inicio; aux != final; aux = aux->sig)
{
aux2 = aux->sig;
free(aux);
aux = aux2;
}
}
Comentarios sobre la versión: Versión 1 (3)