From cf193d86ea8b4ac8f38f6ea70fb0a69b3315da24 Mon Sep 17 00:00:00 2001 From: Vichingo455 Date: Wed, 26 Mar 2025 10:06:46 +0000 Subject: [PATCH] Upload files to "file" --- file/es1.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ file/es2.cpp | 20 ++++++++++ 2 files changed, 123 insertions(+) create mode 100644 file/es1.cpp create mode 100644 file/es2.cpp diff --git a/file/es1.cpp b/file/es1.cpp new file mode 100644 index 0000000..c52c876 --- /dev/null +++ b/file/es1.cpp @@ -0,0 +1,103 @@ +/* +AUTORE: Manuel Vichi 3^AIN +Esercizio 1 stringhe +*/ +#include +#include +#include + +void leggiFileCaratterePerCarattere(const char *nomeFile) { + // Apre il file in lettura + FILE* file = fopen(nomeFile,"r"); + // Verifica se il file è stato aperto correttamente + if (file == NULL) { + perror("l'errore e' stato: "); + } + // Legge il file carattere per carattere, stamparlo a schermo + int ch = 0; + int chn = 0; + while ((ch = fgetc(file))!=EOF) { + putchar(ch); + chn++; + } + // Chiude il file + fclose(file); +} + +int cambiaNomeFile(const char *vecchioNome, const char *nuovoNome) { + // Rinomina il file + if (rename(vecchioNome,nuovoNome) != 0) + return 1; + printf("Il nome del file è stato cambiato da '%s' a '%s'\n", vecchioNome, nuovoNome); + return 0; +} + +int cancellaFile(const char *nomeFile) { + // Elimina il file + if (remove(nomeFile) != 0) + return 1; + printf("Il file '%s' è stato eliminato con successo.\n", nomeFile); + return 0; // Restituisce 0 se l'eliminazione è avvenuta con successo +} + + +void copiaFileCaratterePerCarattere(const char* nomeFileOrigine, const char* nomeFileDestinazione) { + FILE* fileOrigine = fopen(nomeFileOrigine, "r"); + FILE* fileDestinazione = fopen(nomeFileDestinazione, "w"); + if (fileOrigine != NULL) { + int ch = 0; + while ((ch = fgetc(fileOrigine)) != EOF) + fputc((char)ch,fileDestinazione); + } + // Chiudi i file + fclose(fileOrigine); + fclose(fileDestinazione); +} + +void pausa(){ + system("pause"); + system("dir"); + system("pause"); +} + +void copiaPrimi100Caratteri(const char* nomeFileSorgente, const char* nomeFileDestinazione) { + FILE* fileSorgente = fopen(nomeFileSorgente, "r"); + FILE* fileDestinazione = fopen(nomeFileDestinazione, "w"); + if (fileSorgente != NULL) { + int ch = 0; + int chn = 0; + while ((ch = fgetc(fileSorgente)) != EOF && chn != 100) { + fputc((char)ch,fileDestinazione); + chn++; + } + } + // Chiudi i file + fclose(fileSorgente); + fclose(fileDestinazione); +} + +void aggiungiStringa(const char* nomeFile, const char* nome) { + // Apre il file in modalità append (aggiunta in coda) + // Scrive la stringa "BY: nome" in una nuova linea + FILE* file = fopen(nomeFile, "a"); + char nome2[256] = "\nBY: "; + strcat(nome2,nome); + fputs(nome2,file); + // Chiude il file + fclose(file); +} + + +int main() { + leggiFileCaratterePerCarattere("vmware.log"); + pausa(); + copiaFileCaratterePerCarattere("vmware.log", "copia.txt"); + pausa(); + cambiaNomeFile("copia.txt", "log.txt"); + pausa(); + cancellaFile("log.txt"); + pausa(); + copiaPrimi100Caratteri("vmware.log", "copia100.dat"); + aggiungiStringa("copia100.dat", "Manuel Vichi"); + return 0; +} diff --git a/file/es2.cpp b/file/es2.cpp new file mode 100644 index 0000000..e639323 --- /dev/null +++ b/file/es2.cpp @@ -0,0 +1,20 @@ +/* +AUTORE: Manuel Vichi 3^AIN +Esercizio 2 file +*/ +#include +#include +#include +using namespace std; +void leggiFileRigaPerRiga(const char* fileName) { + FILE* file = fopen(fileName, "r"); + char riga[256]; + while(fgets(riga,sizeof(riga),file)) + cout << riga; + cout << endl; + fclose(file); +} +int main(void) { + leggiFileRigaPerRiga("vmware.log"); + return 0; +} \ No newline at end of file