Advertisement
Art_Uspen

Untitled

Dec 7th, 2021
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdint-gcc.h>
  5.  
  6. enum {
  7.     BUFFER_SIZE = 4096
  8. };
  9. struct FileWriteState {
  10.     int fd;           // "файловый дескриптор", для вывода на стандартный поток вывода - 1
  11.     unsigned char *buf;  // указатель на буфер
  12.     int bufsize;         // размер буфера
  13.     unsigned char buf_rec[4096];// здесь потребуется добавить поля для реализации буферизованной записи
  14.     int count;
  15. };
  16.  
  17. void flush(struct FileWriteState *out) {
  18.     write(out->fd, out->buf_rec, BUFFER_SIZE);
  19. }
  20.  
  21. void writechar(int c, struct FileWriteState *out) {
  22.     unsigned char *p = out->buf;
  23.     unsigned char *p_rec = out->buf_rec;
  24.     for (int i = 0; i < c; ++i) {
  25.         if (out->count == BUFFER_SIZE) {
  26.             flush(out);
  27.         }
  28.         *p_rec = *p;
  29.         ++p_rec;
  30.         ++p;
  31.         ++out->count;
  32.     }
  33. }
  34.  
  35.  
  36. static unsigned char buffer[4096];
  37. struct FileWriteState defolt = {STDOUT_FILENO, buffer, BUFFER_SIZE};
  38. struct FileWriteState *stout = &defolt;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement