67 lines
1.3 KiB
C++
67 lines
1.3 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 10/04/2025
|
|
|
|
es10. (filtralinee)
|
|
Scrivere una funzione che accetti come parametri d'ingresso due nomi di
|
|
file e una parola P e memorizzi nel secondo file le sole righe del primo
|
|
che contengono P (suggerimento: usare la funzione strstr).
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
#define SIZE_LINE 1000+1
|
|
|
|
using namespace std;
|
|
|
|
void filtraLinee(const char *nomeFileSorgente, const char *nomeFileDestinazione, char * str);
|
|
|
|
int main(void) {
|
|
char in[SIZE_LINE];
|
|
char out[SIZE_LINE];
|
|
char str[SIZE_LINE];
|
|
|
|
cout << "Input file: ";
|
|
cin >> in;
|
|
|
|
cout << "Output file: ";
|
|
cin >> out;
|
|
|
|
cout << "Search word: ";
|
|
cin >> str;
|
|
|
|
filtraLinee(in, out, str);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void filtraLinee(const char *nomeFileSorgente, const char *nomeFileDestinazione, char * str) {
|
|
FILE *fr = fopen(nomeFileSorgente, "rt");
|
|
|
|
if (fr != NULL) {
|
|
FILE *fw = fopen(nomeFileDestinazione, "wt");
|
|
|
|
if (fw != NULL) {
|
|
char line[SIZE_LINE];
|
|
|
|
while (fgets(line, sizeof(line), fr) != NULL) {
|
|
if (strstr(line, str) != NULL) {
|
|
fputs(line, fw);
|
|
}
|
|
}
|
|
|
|
cout << endl << "Check the output file." << endl;
|
|
|
|
fclose(fw);
|
|
} else {
|
|
perror("Error (destination)");
|
|
}
|
|
|
|
fclose(fr);
|
|
} else {
|
|
perror("Error (source)");
|
|
}
|
|
} |