104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
/*
|
|
AUTORE: Manuel Vichi 3^AIN
|
|
Esercizio 1 stringhe
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <cstring>
|
|
|
|
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;
|
|
}
|