Upload files to "file"
This commit is contained in:
parent
f3ea999cbc
commit
228872bf38
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue