Created
December 18, 2018 23:13
-
-
Save parzibyte/74186fc191f511aac4c029b19dec5d46 to your computer and use it in GitHub Desktop.
Leer archivo en C created by parzibyte - https://repl.it/@parzibyte/Leer-archivo-en-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hola, soy un archivo de texto para demostrar algunos tutoriales de parzibyte.me. Puedo contener cualquier tipo de contenido, por ejemplo, saltos, | |
tabulaciones | |
y cualquier | |
otra | |
cosa. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h>// Todas las funciones como fread, fwrite, fopen, fclose y printf | |
#include <stdlib.h>// EXIT_FAILURE y EXIT_SUCCESS | |
#define TAMANIO_BUFER 16 // Cuántos bytes leer en cada iteración | |
int main() { | |
unsigned char bufer[TAMANIO_BUFER]; | |
FILE *archivo; | |
char *nombreArchivo = "archivo.txt"; | |
size_t bytesLeidos; | |
archivo = fopen(nombreArchivo, "r"); // Abrir en modo read | |
// Si es NULL, entonces no existe, o no se pudo abrir | |
if (!archivo) { | |
printf("¡No se pudo abrir el archivo %s!", nombreArchivo); | |
return EXIT_FAILURE; | |
} | |
// Mientras no alcancemos el EndOfLine del archivo... | |
while (!feof(archivo)) { | |
// Leer dentro del búfer; fread regresa el número de bytes leídos | |
bytesLeidos = fread(bufer, sizeof(char), sizeof(bufer), archivo); | |
// Para depurar, se imprime la cantidad de bytes leídos (se puede quitar esa parte) | |
printf("\nSe han leido %lu bytes:\n", bytesLeidos); | |
// Y el contenido del búfer también es impreso | |
fwrite(bufer, sizeof(char), bytesLeidos, stdout); | |
} | |
// Al final, se cierra el archivo | |
fclose(archivo); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment