# streambuf

***

**1. Reading a File into a Stream**

```cpp
#include <fstream>
#include <streambuf>

int main() {
  std::ifstream file("file.txt");
  if (!file) {
    std::cerr << "Error opening file" << std::endl;
    return 1;
  }

  std::streambuf* buf = file.rdbuf();
  std::string contents(buf->in_avail());
  file.read(&contents[0], contents.size());

  std::cout << contents << std::endl;

  return 0;
}
```

**2. Writing to a File from a Stream**

```cpp
#include <fstream>
#include <streambuf>

int main() {
  std::ofstream file("file.txt");
  if (!file) {
    std::cerr << "Error opening file" << std::endl;
    return 1;
  }

  std::streambuf* buf = file.rdbuf();
  std::string contents = "Hello world!";
  buf->sputn(&contents[0], contents.size());

  file.close();

  return 0;
}
```

**3. Copying Data from One Stream to Another**

```cpp
#include <iostream>
#include <streambuf>

int main() {
  std::ifstream inFile("in.txt");
  std::ofstream outFile("out.txt");
  if (!inFile || !outFile) {
    std::cerr << "Error opening files" << std::endl;
    return 1;
  }

  std::streambuf* inBuf = inFile.rdbuf();
  std::streambuf* outBuf = outFile.rdbuf();

  // Read data from inFile and write it to outFile
  std::streambuf::int_type ch;
  while ((ch = inBuf->sbumpc()) != EOF) {
    outBuf->sputc(ch);
  }

  return 0;
}
```

**4. Creating a Custom Stream Buffer**

```cpp
#include <streambuf>

// Custom stream buffer that uses a string as a buffer
class StringStreambuf : public std::streambuf {
public:
  StringStreambuf(std::string& str) : m_str(str) {}

  int overflow(int c) override {
    m_str += (char)c;
    return c;
  }

private:
  std::string& m_str;
};

// Using the custom stream buffer
int main() {
  std::string str;
  StringStreambuf buf(str);
  std::ostream os(&buf);
  os << "Hello world!" << std::endl;

  std::cout << str << std::endl;

  return 0;
}
```

**5. Using streambuf with std::getline**

```cpp
#include <iostream>
#include <streambuf>

int main() {
  std::string line;
  std::streambuf* buf = std::cin.rdbuf();

  while (std::getline(buf, line)) {
    std::cout << line << std::endl;
  }

  return 0;
}
```

**6. Using streambuf to Read from a Pipe**

```cpp
#include <iostream>
#include <streambuf>
#include <cstdio>

int main() {
  // Create a pipe
  int fd[2];
  pipe(fd);

  // Write to the pipe
  write(fd[1], "Hello world!", 12);

  // Create a streambuf from the pipe read end
  std::streambuf* buf = new std::streambuf(fdopen(fd[0], "r"));

  // Read from the streambuf
  std::string line;
  while (std::getline(buf, line)) {
    std::cout << line << std::endl;
  }

  return 0;
}
```

**7. Using streambuf with a Memory-Mapped File**

```cpp
#include <iostream>
#include <streambuf>

int main() {
  // Create a memory-mapped file
  int fd = open("file.txt", O_CREAT | O_RDWR, 0644);
  char* data = (char*)mmap(nullptr, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

  // Create a streambuf from the memory-mapped file
  std::streambuf* buf = new std::streambuf(data, data + 1024);

  // Read from the streambuf
  std::string line;
  while (std::getline(buf, line)) {
    std::cout << line << std::endl;
  }

  return 0;
}
```

**8. Using streambuf to Read from a Network Socket**

```cpp
#include <iostream>
#include <streambuf>
#include <sys/socket.h>

int main() {
  // Create a socket
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);

  // Connect to a server
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8080);
  connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

  // Create a streambuf from the socket
  std::streambuf* buf = new std::streambuf(fdopen(sockfd, "r"));

  // Read from the streambuf
  std::string line;
  while (std::getline(buf, line)) {
    std::cout << line << std::endl;
  }

  return 0;
}
```

**9. Using streambuf to Write to a Network Socket**

```cpp
#include <iostream>
#include <streambuf>
#include <sys/socket.h>

int main() {
  // Create a socket
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);

  // Connect to a server
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8080);
  connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

  // Create a streambuf from the socket
  std::streambuf* buf = new std::streambuf(fdopen(sockfd, "w"));

  // Write to the streambuf
  buf->sputn("Hello world!", 12);

  return 0;
}
```

**10. Using streambuf to Create a Custom Filter**

```cpp
#include <iostream>
#include <streambuf>
#include <sstream>

// Custom streambuf that filters out certain characters
class FilterStreambuf : public std::streambuf {
public:
  FilterStreambuf(std::streambuf* sb) : m_sb(sb) {}

  int overflow(int c) override {
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      return m_sb->sputc(c);
    } else {
      return m_sb->sbumpc();
    }
  }

private:
  std::streambuf* m_sb;
};

// Using the custom streambuf
int main() {
  std::stringstream ss;
  ss << "Hello world!";

  FilterStreambuf buf(&ss);
  std::ostream os(&buf);

  os << "Hello world!";

  std::string str = ss.str();

  std::cout << str << std::endl;

  return 0;
}
```

**11. Using streambuf to Convert Line Endings**

```cpp
#include <iostream>
#include <streambuf>
#include <sstream>

```
