Upload files to "file"

This commit is contained in:
Vichingo455 2025-03-29 07:35:13 +00:00
parent f3ea999cbc
commit 228872bf38
1 changed files with 36 additions and 0 deletions

36
file/inverti_fauser.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
AUTORE: Manuel Vichi
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>
#include <cctype>
using namespace std;
void invertiFile(const char* fileName) {
FILE* file = fopen(fileName, "r");
if (file != NULL) {
char riga[256];
while (fgets(riga, sizeof(riga), file)) {
int n = strlen(riga);
if (riga[n - 1] == '\n') {
riga[n - 1] = '\0';
n--;
}
for (int i = 0; i < n / 2; i++) {
char temp = riga[i];
riga[i] = riga[n - i - 1];
riga[n - i - 1] = temp;
}
cout << riga << endl;
}
fclose(file);
}
else {
perror("Errore durante la lettura di un file: ");
}
}
int main(void) {
invertiFile("righe.txt");
return 0;
}