68 lines
1.3 KiB
C++
68 lines
1.3 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 04/04/2025
|
|
|
|
es02. (stampavideo2)
|
|
Scrivere una funzione che riceva in ingresso il nome di un
|
|
file e ne stampi a video il contenuto. Utilizzare la funzione
|
|
per mostrare il contenuto dei file in.txt e in2.txt.
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
#define SIZE_LINE 1000+1
|
|
|
|
using namespace std;
|
|
|
|
void apriFile(const char * nomeFile);
|
|
void copiaFile(const char * nomeFileSorgente, const char * nomeFileDestinazione);
|
|
|
|
int main(void) {
|
|
apriFile("in.txt");
|
|
copiaFile("in.txt", "in2.txt");
|
|
|
|
return 0;
|
|
}
|
|
|
|
void apriFile(const char * nomeFile) {
|
|
FILE * fp = fopen(nomeFile, "rt");
|
|
|
|
if (fp != NULL) {
|
|
char str[SIZE_LINE];
|
|
|
|
while (fgets(str, sizeof(str), fp) != NULL) {
|
|
cout << str;
|
|
}
|
|
cout << endl;
|
|
|
|
fclose(fp);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
}
|
|
|
|
void copiaFile(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) {
|
|
fputs(str, fw);
|
|
}
|
|
|
|
fclose(fw);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
|
|
fclose(fr);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
} |