65 lines
1.3 KiB
C
65 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 <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE_LINE 1000+1
|
|
|
|
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];
|
|
|
|
printf("Input file: ");
|
|
scanf("%s", in);
|
|
|
|
printf("Output file: ");
|
|
scanf("%s", out);
|
|
|
|
printf("Search word: ");
|
|
scanf("%s", 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);
|
|
}
|
|
}
|
|
|
|
printf("\nCheck the output file.\n");
|
|
|
|
fclose(fw);
|
|
} else {
|
|
perror("Error (destination)");
|
|
}
|
|
|
|
fclose(fr);
|
|
} else {
|
|
perror("Error (source)");
|
|
}
|
|
} |