Skip to content

Instantly share code, notes, and snippets.

@parzibyte

parzibyte/leer.c Secret

Created October 30, 2020 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/feebaf7c2e606678d00e193008dfad0a to your computer and use it in GitHub Desktop.
Save parzibyte/feebaf7c2e606678d00e193008dfad0a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
// Debe ser de la máxima + 1. Por ejemplo, si la máxima es 4, defínela como 5
#define MAXIMA_LONGITUD_CADENA 1000
// La máxima cantidad de líneas que puede tener
#define CANTIDAD_LINEAS 20
#define NOMBRE_ARCHIVO "archivo.txt"
/*
https://parzibyte.me/blog
*/
int main()
{
// Arreglo de cadenas: aquí almacenamos todas las palabras
char palabras[CANTIDAD_LINEAS][MAXIMA_LONGITUD_CADENA];
// Útil para leer el archivo
char buferArchivo[MAXIMA_LONGITUD_CADENA];
// Abrir el archivo...
FILE *archivo = fopen(NOMBRE_ARCHIVO, "r");
if (archivo == NULL)
{
printf("No se puede abrir el archivo");
return 0;
}
// Necesitamos este ayudante para saber en qué línea vamos
int indice = 0;
// Mientras podamos leer una línea del archivo
while (fgets(buferArchivo, MAXIMA_LONGITUD_CADENA, archivo))
{
// Remover salto de línea
strtok(buferArchivo, "\n");
// Copiar la línea a nuestro arreglo, usando el índice
memcpy(palabras[indice], buferArchivo, MAXIMA_LONGITUD_CADENA);
// Aumentarlo en cada iteración
indice++;
}
// Terminamos de leer
fclose(archivo);
// Ahora ya tenemos el arreglo. Podemos imprimirlo
int i;
for (i = 0; i < CANTIDAD_LINEAS; i++)
{
printf("Tenemos una línea: '%s'\n", palabras[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment