Aula 13 - Programa para Leitura e para Cópia de ARQUIVO #4

Open
opened 2025-04-16 22:06:18 -03:00 by NRZCode · 0 comments

Um programa estilo cat.

Esse programa lê o conteúdo de um arquivo passado como primeiro argumento e imprime na saída padrão.

#include <stdio.h>

void print_bytes(char *buf) {
    int i = 0;
    while(1) {
        printf("%02x ", (unsigned char)buf[i]);
        if (buf[i] == '\0') {
            break;
        }
        i++;
    }
    putchar('\n');
}

#define MAXBUF 4096
// BUFSIZ = 8192

int main(int argc, char **argv) {
    if (argc < 2)
        return 1;
    char buf[BUFSIZ];
    FILE *arquivo = fopen(argv[1], "r");
    while (fgets(buf, BUFSIZ, arquivo) != NULL) {
        // fprintf(stdout, "%s", buf);
        // fputs(buf, stdout);
        printf("%s", buf);
    }
    fclose(arquivo);
    return 0;
}

Um programa estilo cp

Esse programa lê o conteúdo de um arquivo passado como primeiro argumento e o copia para outro arquivo passado como segundo argumento.

#include <stdio.h>

void print_bytes(char *buf) {
    int i = 0;
    while(1) {
        printf("%02x ", (unsigned char)buf[i]);
        if (buf[i] == '\0') {
            break;
        }
        i++;
    }
    putchar('\n');
}

#define MAXBUF 4096
// BUFSIZ = 8192

int main(int argc, char **argv) {
    if (argc < 3)
        return 1;
    char buf[BUFSIZ];
    FILE *origem = fopen(argv[1], "r");
    FILE *destino = fopen(argv[2], "w");
    while (fgets(buf, BUFSIZ, origem) != NULL) {
        // fprintf(destino, "%s", buf);
        fputs(buf, destino);
    }
    fclose(origem);
    fclose(destino);
    return 0;
}
## Um programa estilo `cat`. Esse programa lê o conteúdo de um arquivo passado como primeiro argumento e imprime na saída padrão. ```c #include <stdio.h> void print_bytes(char *buf) { int i = 0; while(1) { printf("%02x ", (unsigned char)buf[i]); if (buf[i] == '\0') { break; } i++; } putchar('\n'); } #define MAXBUF 4096 // BUFSIZ = 8192 int main(int argc, char **argv) { if (argc < 2) return 1; char buf[BUFSIZ]; FILE *arquivo = fopen(argv[1], "r"); while (fgets(buf, BUFSIZ, arquivo) != NULL) { // fprintf(stdout, "%s", buf); // fputs(buf, stdout); printf("%s", buf); } fclose(arquivo); return 0; } ``` ## Um programa estilo `cp` Esse programa lê o conteúdo de um arquivo passado como primeiro argumento e o copia para outro arquivo passado como segundo argumento. ```c #include <stdio.h> void print_bytes(char *buf) { int i = 0; while(1) { printf("%02x ", (unsigned char)buf[i]); if (buf[i] == '\0') { break; } i++; } putchar('\n'); } #define MAXBUF 4096 // BUFSIZ = 8192 int main(int argc, char **argv) { if (argc < 3) return 1; char buf[BUFSIZ]; FILE *origem = fopen(argv[1], "r"); FILE *destino = fopen(argv[2], "w"); while (fgets(buf, BUFSIZ, origem) != NULL) { // fprintf(destino, "%s", buf); fputs(buf, destino); } fclose(origem); fclose(destino); return 0; } ```
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: blau_araujo/cblc#4
No description provided.