forked from DmitriyKustov/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.c
More file actions
39 lines (33 loc) · 1021 Bytes
/
save.c
File metadata and controls
39 lines (33 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* save.c -- реализует команду сохранения текста
*
* Copyright (c) 2018, Darya Madrakhimova <madrahim@petrsu.ru>
*
* This code is licensed under a MIT-style license.
*/
#include "common.h"
#include "text/text.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static void save_line(int index, char *contents, int cursor, void *data);
/**
* Сохраняет текст в указанный файл
*/
void save(text txt, char *filename) {
FILE *f;
/* Открываем файл для записи, контролируя ошибки */
if ((f = fopen(filename, "w")) == NULL) {
printf("The file %s cannot be opened\n", filename);
return;
}
process_forward(txt, save_line, f);
}
static void save_line(int index, char *contents, int cursor, void *data) {
assert(contents != NULL);
UNUSED(index);
UNUSED(cursor);
/* Записываем строку в файл */
fprintf((FILE *)data, "%s", contents);
fflush((FILE *)data);
}