School-Coding-Cpp/sfusi/es11_accodalinee_foschini_f...

58 lines
1.2 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 09/04/2025
es11. (accodalinee)
Scrivere un programma che, chiesto in input il nome di un file di testo, accodi le
linee del file al file di output risultato.txt, sostituendo le linee più lunghe di
20 caratteri con la stringa --LINEA TROPPO LUNGA--.
*/
#include <iostream>
#define SIZE_LINE 1000+1
using namespace std;
void apriFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
int main(void) {
char nomeFile[SIZE_LINE];
cout << "Nome file: ";
cin >> nomeFile;
apriFile(nomeFile, "risultato.txt");
return 0;
}
void apriFile(const char * nomeFileSorgente, const char * nomeFileDestinazione) {
FILE * fr = fopen(nomeFileSorgente, "rt");
if (fr != NULL) {
FILE * fw = fopen(nomeFileDestinazione, "wt");
if (fw != NULL) {
char str[SIZE_LINE];
while (fgets(str, sizeof(str), fr) != NULL) {
if (strlen(str) > 20) {
fputs("--LINEA TROPPO LUNGA--\n", fw);
} else {
fputs(str, fw);
}
}
fclose(fw);
} else {
perror("Error (destination)");
}
fclose(fr);
} else {
perror("Error (source)");
}
}