fgrieu

fast stdin/stdout

Sep 5th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | Source Code | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. #define BUF_SIZE 4096
  5.  
  6. int main() {
  7.     DWORD bytesRead;
  8.     char buffer[BUF_SIZE];
  9.  
  10.     // Ensure stdin is in binary mode
  11.     _setmode(_fileno(stdin), _O_BINARY);
  12.  
  13.     HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  14.  
  15.     while (ReadFile(hStdin, buffer, BUF_SIZE, &bytesRead, NULL) && bytesRead > 0) {
  16.         // Process buffer[0..bytesRead-1]
  17.     }
  18.  
  19.     return 0;
  20. }
  21.  
  22.  
  23. #include <unistd.h>
  24.  
  25. #define BUF_SIZE 4096
  26.  
  27. int main() {
  28.     char buffer[BUF_SIZE];
  29.     ssize_t bytesRead;
  30.  
  31.     while ((bytesRead = read(STDIN_FILENO, buffer, BUF_SIZE)) > 0) {
  32.         // Process buffer[0..bytesRead-1]
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <stdint.h>
  42.  
  43. #ifdef _WIN32
  44.   #include <io.h>      // _setmode(), _fileno()
  45.   #include <fcntl.h>   // _O_BINARY
  46.   #include <windows.h> // for GetLastError()
  47. #else
  48.   #include <unistd.h>  // write(), STDOUT_FILENO, usleep()
  49.   #include <errno.h>   // errno
  50. #endif
  51.  
  52. // write_all: repeatedly write() or fwrite() until all bytes are out
  53. static int write_all(const uint8_t *buf, size_t total) {
  54.     size_t written = 0;
  55.     while (written < total) {
  56.   #ifdef _WIN32
  57.         int n = _write(_fileno(stdout),
  58.                        buf + written,
  59.                        (unsigned int)(total - written));
  60.         if (n < 0) {
  61.             fprintf(stderr,
  62.                     "write error: code %ld\n",
  63.                     GetLastError());
  64.             return -1;
  65.         }
  66.   #else
  67.         ssize_t n = write(STDOUT_FILENO,
  68.                           buf + written,
  69.                           total - written);
  70.         if (n < 0) {
  71.             if (errno == EINTR) continue;  // retry on signal
  72.             perror("write");
  73.             return -1;
  74.         }
  75.   #endif
  76.         written += (size_t)n;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. int main(void) {
  82.     // Example: allocate a 100 MiB buffer and fill it with a pattern
  83.     size_t bufsize = 100 * 1024 * 1024;
  84.     uint8_t *buffer = malloc(bufsize);
  85.     if (!buffer) {
  86.         perror("malloc");
  87.         return EXIT_FAILURE;
  88.     }
  89.     for (size_t i = 0; i < bufsize; i++)
  90.         buffer[i] = (uint8_t)(i & 0xFF);
  91.  
  92.   #ifdef _WIN32
  93.     // Switch stdout to binary mode on Windows
  94.     if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
  95.         fprintf(stderr, "failed to set stdout to binary mode\n");
  96.         free(buffer);
  97.         return EXIT_FAILURE;
  98.     }
  99.   #endif
  100.  
  101.     if (write_all(buffer, bufsize) != 0) {
  102.         free(buffer);
  103.         return EXIT_FAILURE;
  104.     }
  105.  
  106.     // Optionally flush to ensure all data is handed off
  107.     fflush(stdout);
  108.  
  109.     free(buffer);
  110.     return EXIT_SUCCESS;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment