diff --git a/c/files/files1/count b/c/files/files1/count new file mode 100755 index 0000000..2dc527b Binary files /dev/null and b/c/files/files1/count differ diff --git a/c/files/files1/datos.txt b/c/files/files1/datos.txt new file mode 100644 index 0000000..c917265 --- /dev/null +++ b/c/files/files1/datos.txt @@ -0,0 +1,3 @@ +Hola, mundo +C es genial +Fin diff --git a/c/files/files1/main.c b/c/files/files1/main.c new file mode 100644 index 0000000..f110fab --- /dev/null +++ b/c/files/files1/main.c @@ -0,0 +1,50 @@ +#include +#include + +int count_chars(char *rline); +void fix_path(char *rpath); + +int main(void) { + char file_path[128]; + int char_count = 0, lines_count = 0; + FILE *fp; + + printf("Path:"); + if (fgets(file_path, 128, stdin) == NULL) { + perror("fgets"); + return 1; + } + printf("\nLeyendo archivo: %s", file_path); + + fix_path(file_path); + + fp = fopen(file_path, "r"); + + if (fp == NULL) { + perror("fopen"); + return 1; + } + + char lbuffer[256]; + + while ((fgets(lbuffer, 256, fp)) != NULL) { + char_count += count_chars(lbuffer); + lines_count++; + } + + printf("Caracteres: %d\n", char_count); + printf("Lineas: %d\n", lines_count); + + return 0; +} + +int count_chars(char *rline) { return strlen(rline) - 1; } + +void fix_path(char *rpath) { + for (int i = 0; i < 128; i++) { + if (rpath[i] == '\n') { + rpath[i] = '\0'; + return; + } + } +}