68 lines
1.3 KiB
C++
68 lines
1.3 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 10/04/2025
|
|
|
|
es09. (inverti)
|
|
Scrivere un programma che inverta ogni riga contenuta nel file righe.txt e
|
|
riporti il risultato sullo schermo (per esempio, la riga "Prova di stampa"
|
|
diventa "apmats id avorP").
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
#define SIZE 1000+1
|
|
|
|
using namespace std;
|
|
|
|
void creaFile(const char *nomeFile, const char *nomeFileDestinazione);
|
|
void invertiRighe(char *str);
|
|
|
|
int main(void) {
|
|
creaFile("righe.txt", "righe_invertite.txt");
|
|
|
|
return 0;
|
|
}
|
|
|
|
void creaFile(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];
|
|
|
|
while (fgets(str, sizeof(str), fr) != NULL) {
|
|
char *pos;
|
|
|
|
if ((pos = strchr(str, '\n')) != NULL) {
|
|
*pos = '\0';
|
|
}
|
|
|
|
invertiRighe(str);
|
|
fputs(str, fw);
|
|
|
|
if (*pos == '\0') {
|
|
fputs("\n", fw);
|
|
}
|
|
}
|
|
|
|
fclose(fw);
|
|
}
|
|
|
|
fclose(fr);
|
|
} else {
|
|
perror("Error");
|
|
}
|
|
}
|
|
|
|
void invertiRighe(char *str) {
|
|
for (char *pl = str, *pr = str + strlen(str) - 1; pl < pr; pl++, pr--) {
|
|
char temp = *pl;
|
|
*pl = *pr;
|
|
*pr = temp;
|
|
}
|
|
} |