Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- #define MAX 100
- #define INDIETRO "indietro"
- #define FINE "fine"
- FILE *file_open(char *filename, char *mode) {
- FILE *fp = fopen(filename, mode);
- if (fp == NULL) {
- printf("Errore nell'apertura di %s\n", filename);
- exit(1);
- }
- return fp;
- }
- int main() {
- char buffer[MAX];
- FILE *fp = file_open("file.txt", "w");
- long pos, new_pos = 0;
- // gets(buffer)
- while (1) {
- printf("Cosa si desidera scrivere nel file? ");
- //fgets(buffer, MAX, stdin);
- gets(buffer);
- if (strcmp(buffer, INDIETRO) == 0) {
- new_pos = pos;
- printf("Ritorno al byte %ld...\n", pos);
- fseek(fp, pos, SEEK_SET);
- } else {
- printf("Scrittura di \"%s\". ", buffer);
- pos = new_pos;
- fprintf(fp, "%s\n", buffer);
- new_pos = ftell(fp);
- printf("Passaggio dal byte %ld al byte %ld...\n", pos, new_pos);
- if (strcmp(buffer, FINE) == 0)
- break;
- }
- }
- printf("Troncamento del file al byte %ld...\n", pos);
- ftruncate(fileno(fp), pos);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment