> For the complete documentation index, see [llms.txt](https://dimitar-minchev.gitbook.io/linux-system-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dimitar-minchev.gitbook.io/linux-system-programming/03_files/10_writefile.md).

# Запис на изречения във файл

Създайте празен файл **sentences.txt**, в случай на неуспех отпечатайте грешка и излезте. Направете 100 пъти следното: извикайте **getSentence()**, за да получите нов текст в буфера и неговата дължина, запишете буфера във файла и запишете броя на байтове. Ако броят на записаните байтове е -1, отпечатайте грешка и излезте. Затворете файла, при неуспех изведете грешка.

## writefile.c

```c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h>
#define BUF_SIZE 1000000
long getMicrotime(){
        struct timeval currentTime;
        gettimeofday(&currentTime, NULL);
        return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec;
}
int getSentence(char *buf) {
    sprintf(buf, "%ld> This is a new sentence.\n", getMicrotime());
    return strlen(buf);
}
int main(int argc, char *argv[])
{
    int fd;
    fd = open("./sentences.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1)
    {
        printf("ERROR opening the file!\n");
        return -1;
    }
    char buf[BUF_SIZE];
    for (int i=0; i < 100; i++)
    {
        int text_len = getSentence(buf);
        ssize_t nr = write(fd, buf, text_len);
        if (nr == -1)
        {
            printf("ERROR writing to the file!\n");
            break;
        }
    }
    if (close(fd) == -1)
    {
        printf("ERROR closing the file!\n");
        return -2;
    }
    printf("SUCCESS!\n");
    return 0;
}
```

Компилирайте и стартирайте програмата, след което изведете съдържанието на файла **sentences.txt**:

```
gcc writefile.c -o writefile
./writefile
cat sentences.txt
```
