# istream

***

**1. Reading Characters**

```cpp
#include <iostream>

int main() {
  char ch;
  std::cin >> ch;  // Reads a single character from standard input
  std::cout << ch << std::endl;
  return 0;
}
```

**2. Reading a Line of Text**

```cpp
#include <iostream>

int main() {
  std::string line;
  std::getline(std::cin, line);  // Reads a line of text (including spaces) from standard input
  std::cout << line << std::endl;
  return 0;
}
```

**3. Reading an Integer**

```cpp
#include <iostream>

int main() {
  int num;
  std::cin >> num;  // Reads an integer from standard input
  std::cout << num << std::endl;
  return 0;
}
```

**4. Reading a Floating-Point Number**

```cpp
#include <iostream>

int main() {
  double num;
  std::cin >> num;  // Reads a floating-point number from standard input
  std::cout << num << std::endl;
  return 0;
}
```

**5. Reading a Boolean**

```cpp
#include <iostream>

int main() {
  bool flag;
  std::cin >> flag;  // Reads a boolean (true/false) from standard input
  std::cout << flag << std::endl;
  return 0;
}
```

**6. Reading from a File**

```cpp
#include <iostream>
#include <fstream>

int main() {
  std::ifstream file("input.txt");
  char ch;
  while (file >> ch) {
    // Process character ch
  }
  file.close();
  return 0;
}
```

**7. Reading from stdin (Standard Input)**

```cpp
#include <iostream>

int main() {
  char ch;
  while (std::cin >> ch) {
    // Process character ch
  }
  return 0;
}
```

**8. Checking for End of File (EOF)**

```cpp
#include <iostream>
#include <fstream>

int main() {
  std::ifstream file("input.txt");
  char ch;
  while (!file.eof()) {
    file >> ch;
    // Process character ch
  }
  file.close();
  return 0;
}
```

**9. Ignoring White Space**

```cpp
#include <iostream>

int main() {
  char ch;
  std::cin >> std::ws >> ch;  // Ignores white space before reading character ch
  std::cout << ch << std::endl;
  return 0;
}
```

**10. Using Delimiters**

```cpp
#include <iostream>

int main() {
  std::string line;
  std::getline(std::cin, line, ',');  // Reads a line of text and stops at the first comma
  std::cout << line << std::endl;
  return 0;
}
```

**11. Reading Binary Data**

```cpp
#include <iostream>
#include <fstream>

int main() {
  std::ifstream file("binary.bin", std::ios::binary);
  char buffer[1024];
  file.read(buffer, sizeof(buffer));  // Reads 1024 bytes of binary data into buffer
  file.close();
  return 0;
}
```

**12. Writing Characters**

```cpp
#include <iostream>

int main() {
  char ch = 'a';
  std::cout << ch << std::endl;  // Writes character ch to standard output
  return 0;
}
```

**13. Writing a Line of Text**

```cpp
#include <iostream>

int main() {
  std::string line = "Hello world!";
  std::cout << line << std::endl;  // Writes a line of text to standard output
  return 0;
}
```

**14. Writing an Integer**

```cpp
#include <iostream>

int main() {
  int num = 123;
  std::cout << num << std::endl;  // Writes integer num to standard output
  return 0;
}
```

**15. Writing a Floating-Point Number**

```cpp
#include <iostream>

int main() {
  double num = 3.14;
  std::cout << num << std::endl;  // Writes floating-point number num to standard output
  return 0;
}
```

**16. Writing a Boolean**

```cpp
#include <iostream>

int main() {
  bool flag = true;
  std::cout << flag << std::endl;  // Writes boolean flag to standard output
  return 0;
}
```

**17. Writing to a File**

```cpp
#include <iostream>
#include <fstream>

int main() {
  std::ofstream file("output.txt");
  char ch = 'a';
  file << ch << std::endl;  // Writes character ch to file output.txt
  file.close();
  return 0;
}
```

**18. Writing to stdout (Standard Output)**

```cpp
#include <iostream>

int main() {
  char ch = 'a';
  std::cout << ch << std::endl;  // Writes character ch to standard output
  return 0;
}
```

**19. Formatting Output**

```cpp
#include <iostream>

int main() {
  int num = 123;
  std::cout << std::setw(10) << num << std::endl;  // Formats output to 10 characters wide
  return 0;
}
```

**20. Using Manipulators**

```cpp
#include <iostream>

int main() {
  int num = 123;
  std::cout << std::hex << num << std::endl;  // Formats output as hexadecimal
  return 0;
}
```

**21. Overloading the >> Operator**

```cpp
class MyCustomClass {
public:
  MyCustomClass(int value) : value(value) {}
  friend std::istream& operator>>(std::istream& in, MyCustomClass& customClass) {
    in >> customClass.value;
    return in;
  }

private:
  int value;
};

int main() {
  MyCustomClass customClass;
  std::cin >> customClass;  // Reads MyCustomClass from standard input
  return 0;
}
```

**22. Overloading the << Operator**

```cpp
class MyCustomClass {
public:
  MyCustomClass(int value) : value(value) {}
  friend std::ostream& operator<<(std::ostream& out, const MyCustomClass& customClass) {
    out << customClass.value;
    return out;
  }

private:
  int value;
};

int main() {
  MyCustomClass customClass(123);
  std::cout << customClass << std::endl;  // Writes MyCustomClass to standard output
  return 0;
}
```

**23. Using std::stringstream**

```cpp
#include <sstream>

int main() {
  std::stringstream ss;
  ss << 123;  // Writes integer 123 to string stream
  std::string str = ss.str();  // Retrieves string from string stream
  return 0;
}
```

**24. Reading from a String**

```cpp
#include <sstream>

int main() {
  std::string str = "123";
  std::stringstream ss(str);
  int num;
  ss >> num;  // Reads integer from string stream
  return 0;
}
```

**25. Writing to a String**

```cpp
#include <sstream>

int main() {
  int num = 123;
  std::stringstream ss;
  ss << num;  // Writes integer to string stream
  std::string str = ss.str();  // Retrieves string from string stream
  return 0;
}
```

**26. Using std::getline with Delimiters**

```cpp
#include <iostream>

int main() {
  std::string line;
  while (std::getline(std::cin, line, '\n')) {
    // Process line
  }
  return 0;
}
```

**27. Using std::getline with Character Count**

```cpp
#include <iostream>

int main() {
  std::string line;
  while (std::getline(std::cin, line, 10)) {
    // Process line (truncated to 10 characters)
  }
  return 0;
}
```

**28. Reading from a Pipe**

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

int main() {
  FILE* pipe = popen("ls -l", "r");  // Opens pipe to process ls -l
  char buffer[1024];
  fread(buffer, sizeof(buffer), 1, pipe);  // Reads from pipe
  pclose(pipe);  // Closes pipe
  return 0;
}
```

**29. Reading from a Socket**

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

int main() {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);  // Creates socket
  struct sockaddr_in serverAddr;  // Server address
  connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));  // Connects to server
  char buffer[1024];
  recv(sockfd, buffer, sizeof(buffer), 0);  // Reads from socket
  close(sockfd);  // Closes socket
  return 0;
}
```

**30. Reading from a Device**

```cpp
#include <iostream>
#include <fcntl.h>

int main() {
  int fd = open("/dev/ttyS0", O_RDONLY);  // Opens serial port
  char buffer[1024];
  read(fd, buffer, sizeof(buffer));  // Reads from device
  close(fd);  // Closes device
  return 0;
}
```

**31. Reading from a Web Page**

```cpp
#include <iostream>
#include <curl/curl.h>

int main() {
  CURL* curl = curl_easy_init();  // Initializes cURL
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");  // Sets URL
  std::string response;
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &responseWriter);  // Sets write callback
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);  // Sets write data
  curl_easy_perform(curl);  // Performs request
  curl_easy_cleanup(curl);  // Cleans up cURL

  std::cout << response << std::endl;  // Outputs response
  return 0;
}

size_t responseWriter(void* ptr, size_t size, size_t nmemb, void* data) {
  std::string* response = (std::string*)data;
  response->append((char*)ptr, size * nmemb);
  return size * nmemb;
}
```

**32. Reading from a Database**

```cpp
#include <iostream>
#include <mysql.h>

int main() {
  MYSQL* mysql = mysql_init(NULL);  // Initializes MySQL
  mysql_real_connect(mysql, "localhost", "username", "password", "database", 0, NULL, 0);  // Connects to database
  MYSQL_RES* result = mysql_store_result(mysql);  // Executes query and stores result

  std::string row;
  while (MYSQL_ROW mysqlRow = mysql_fetch_row(result)) {
    for (int i = 0; i < mysql_field_count(mysql); i++) {
      row.append(mysqlRow[i]);
      row.append(" ");
    }
  }
  std::cout << row << std::endl;  // Outputs data

  mysql_free_result(result);  // Frees result
  mysql_close(mysql);  // Closes connection
  return 0;
}
```

**33. Reading from a JSON File**

```cpp
#include <iostream>
#include <json/json.h>

int main() {
  Json::Value root;  // JSON root object
  Json::Reader reader;  // JSON reader

  std::ifstream ifs("data.json");  // JSON file input stream
  if (!reader.parse(ifs, root)) {
    std::cerr << "Error parsing JSON file" << std::endl;
    return 1;
  }

  std::string value = root["key"].asString();  // Retrieves JSON value
  std::cout << value << std::endl;  // Outputs value
  return 0;
}
```

**34. Reading from an XML File**

```cpp
#include <iostream>
#include <tinyxml2.h>

int main() {
  tinyxml2::XMLDocument doc;  // XML document
  if (doc.LoadFile("data.xml") != tinyxml2::XML_SUCCESS) {
    std::cerr << "Error loading XML file" << std::endl;
    return 1;
  }

  tinyxml2::XMLNode* root = doc.FirstChild();  // XML root element
  std::string value = root->FirstChildElement("key")->GetText();  // Retrieves XML value
  std::cout << value << std::endl;  // Outputs value
  return 0;
}
```

**35. Reading from an INI File**

```cpp
#include <iostream>
#include <boost/property_tree/ini_parser.hpp>

int main() {
  boost::property_tree::ini_parser::read_ini("data.ini", prop);  // Parses INI file
  std::string value = prop.get<std::string>("key");  // Retrieves INI property
  std::cout << value << std::endl;  // Outputs property
  return 0;
}
```

**36. Reading from a YAML File**

```cpp
#include <iostream>
#include <yaml-cpp/yaml.h>

int main() {
  YAML::Node root = YAML::LoadFile("data.yaml");  // Parses YAML file
  std::string value = root["key"].as<std::string>();  // Retrieves YAML value
  std::cout << value << std::endl;  // Outputs value
  return 0;
}
```

**37. Reading from a CSV File**

```cpp
#include <iostream>
#include <fstream>
#include <vector>

int main() {
  std::ifstream ifs("data.csv");  // CSV file input stream
  std::vector<std::string> fields;
  std::string line;
  while (std::getline(ifs, line)) {
    std::stringstream ss(line);
    std::string field;
    while (std::getline(ss, field, ',')) {
      fields.push_back(field);
    }
  }

  // Process fields
  return 0;
}
```

**38. Reading from a Zip Archive**

```cpp
#include <iostream>
#include <fstream>
#include <zlib.h>

int main() {
  std::ifstream ifs("data.zip", std::ios::binary);  // Zip archive input stream
  z_stream stream;  // Zlib stream
  stream.zalloc = Z_NULL;
  stream.zfree = Z_NULL;
  stream.opaque = Z_NULL;
  inflateInit(&stream);  // Initializes Zlib

  char buffer[1024];
  while (ifs.read(buffer, sizeof(buffer))) {
    stream.avail_in = ifs.gcount();
    stream.next_in = (Bytef*)buffer;
    do {
      stream.avail_out = sizeof(buffer);
      stream.next_out = (Bytef*)buffer;
      inflate(&stream, Z_NO_FLUSH);  // Inflates data
    } while (stream.avail_out == 0);
  }

  inflateEnd(&stream);  // Finalizes Zlib

  std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
  ofs.write(buffer, sizeof(buffer));  // Writes decompressed data
  return 0;
}
```

**39. Reading from a GZip Archive**

```cpp
#include <iostream>
#include <fstream>
#include <zlib.h>

int main() {
  std::ifstream ifs("data.gz", std::ios::binary);  // GZip archive input stream
  z_stream stream;  // Zlib stream
  stream.zalloc = Z_NULL;
  stream.zfree = Z_NULL;
  stream.opaque = Z_NULL;
  inflateInit2(&stream, 16+MAX_WBITS);  // Initializes Zlib with GZip header detection

  char buffer[1024];
  while (ifs.read(buffer, sizeof(buffer))) {
    stream.avail_in = ifs.gcount();
    stream.next_in = (Bytef*)buffer;
    do {
      stream.avail_out = sizeof(buffer);
      stream.next_out = (Bytef*)buffer;
      inflate(&stream, Z_NO_FLUSH);  // Inflates data
    } while (stream.avail_out == 0);
  }

  inflateEnd(&stream);  // Finalizes Zlib

  std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
  ofs.write(buffer, sizeof(buffer));  // Writes decompressed data
  return 0;
}
```

**40. Reading from a BZip2 Archive**

```cpp
#include <iostream>
#include <fstream>
#include <bzlib.h>

int main() {
  std::ifstream ifs("data.bz2", std::ios::binary);  // BZip2 archive input stream
  BZFILE* bzf = BZ2_bzReadOpen(&ifs, 0, 0, 0, NULL, 0);  // Initializes BZip2

  char buffer[1024];
  while (BZ2_bzRead(&bzf, buffer, sizeof(buffer)) > 0) {
    std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
    ofs.write(buffer, sizeof(buffer));  // Writes decompressed data
  }

  BZ2_bzReadClose(&bzf);  // Finalizes BZip2
  return 0;
}
```

**41. Reading from a Tar Archive**

```cpp
#include <iostream>
#include <fstream>
#include <tar.h>

int main() {
  std::ifstream ifs("data.tar", std::ios::binary);  // Tar archive input stream
  int fd = ifs.rdbuf()->fd();  // File descriptor

  tar_t* tar = tar_open(fd, O_RDONLY, NULL);  // Initializes Tar

  const char* filename;
  char buffer[1024];
  while (tar_next(tar, &filename)) {
    std::ofstream ofs(filename, std::ios::binary);  // Extracted file output stream
    while (tar_read(tar, buffer, sizeof(buffer)) > 0) {
      ofs.write(buffer, sizeof(buffer));  // Writes extracted data
    }
  }

  tar_close(tar);  // Finalizes Tar
  return 0;
}
```

**42. Reading from a Rar Archive**

```cpp
#include <iostream>
#include <fstream>
#include <unrar.h>

int main() {
  std::ifstream ifs("data.rar", std::ios::binary);  // Rar archive input stream
  int fd = ifs.rdbuf()->fd();  // File descriptor

  rar_t* rar = NewRAR();  // Initializes Rar
  if (RAROpenArchive(rar, fd, 0, NULL)) {
    int fileCount = RARGetArchiveSize(rar);
    for (int i = 0; i < fileCount; i++) {
      RarHeaderData* rarHeaderData;
      RARGetHeaderData(rar, i, &rarHeaderData);
      char buffer[1024];
      while (RARReadHeader(rar)) {
        while (RARReadData(rar, buffer, sizeof(buffer)) > 0) {
          std::ofstream ofs(rarHeaderData->FileName, std::ios::binary);  // Extracted file output stream
          ofs.write(buffer, sizeof(buffer));  // Writes extracted data
        }
      }
    }
    RARCloseArchive(rar);  // Finalizes Rar
  }
  DeleteRAR(rar);  // Frees Rar resources
  return 0;
}
```

**43. Reading from a 7-Zip Archive**

```cpp
#include <iostream>
#include <fstream>
#include <7z.h>

int main() {
  std::ifstream ifs("data.7z", std::ios::binary);  // 7-Zip archive input stream
  int fd = ifs.rdbuf()->fd();  // File descriptor

  ISzAlloc* alloc = SzAlloc();  // Initializes 7-Zip
  ISzExtract* extract = SzExtract();

  CFileInStream archiveStream(fd);
  CArchiveDatabase archiveDatabase;
  if (extract->Open(&archiveStream, fd, alloc) == SZ_OK) {
    int fileCount = archiveDatabase.Database.NumFiles;
    for (int i = 0; i < fileCount; i++) {
      CFileItem fileItem = archiveDatabase.Database.Files[i];
      char buffer[1024];
      while (extract->ReadData(extract, &fileItem, buffer, sizeof(buffer))) {
        std::ofstream ofs(fileItem.Name, std::ios::binary);  // Extracted file output stream
        ofs.write(buffer, sizeof(buffer));  // Writes extracted data
      }
    }
    extract->Close(extract);  // Finalizes 7-Zip
  }
  SzFree(extract);  // Frees 7-Zip resources
  SzFree(alloc);  // Frees 7-Zip resources
  return 0;
}
```

**44. Reading from a Zip64 Archive**

```cpp
#include <iostream>
#include <fstream>
#include <zlib.h>

int main() {
  std::ifstream ifs("data.zip64", std::ios::binary);  // Zip64 archive input stream
  z_stream stream;  // Zlib stream
  stream.zalloc = Z_NULL;
  stream.zfree = Z_NULL;
  stream.opaque = Z_NULL;
  inflateInit64(&stream);  // Initializes Zlib with Zip64 support

  char buffer[1024];
  while (ifs.read(buffer, sizeof(buffer))) {
    stream.avail_in = ifs.gcount();
    stream.next_in = (Bytef*)buffer;
    do {
      stream.avail_out = sizeof(buffer);
      stream.next_out = (Bytef*)buffer;
      inflate64(&stream, Z_NO_FLUSH);  // Inflates data
    } while (stream.avail_out == 0);
  }

  inflateEnd64(&stream);  // Finalizes Zlib

  std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
  ofs.write(buffer, sizeof(buffer));  // Writes decompressed data
  return 0;
}
```

**45. Reading from a Brotli Archive**

```cpp
#include <iostream>
#include <fstream>
#include <brotli/decode.h>

int main() {
  std::ifstream ifs("data.brotli", std::ios::binary);  // Brotli archive input stream
  std::vector<uint8_t> bytes(ifs.rdbuf()->in_avail());
  ifs.read((char*)bytes.data(), sizeof(uint8_t) * bytes.size());

  size_t sizeOut = 0;
  BrotliDecoderResult result = BrotliDecoderDecompress(bytes.size(), bytes.data(), &sizeOut, NULL, NULL);
  if (result == BROTLI_DECODER_RESULT_SUCCESS) {
    std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
    ofs.write((char*)bytes.data(), sizeOut);  // Writes decompressed data
  }
  return 0;
}
```

**46. Reading from a Zstd Archive**

```cpp
#include <iostream>
#include <fstream>
#include <zstd.h>

int main() {
  std::ifstream ifs("data.zstd", std::ios::binary);  // Zstd archive input stream
  std::vector<uint8_t> bytes(ifs.rdbuf()->in_avail());
  ifs.read((char*)bytes.data(), sizeof(uint8_t) * bytes.size());

  size_t sizeOut = ZSTD_getFrameContentSize(bytes.data(), bytes.size());
  std::vector<uint8_t> decompressed(sizeOut);
  size_t decompressedSize = ZSTD_decompress(decompressed.data(), decompressed.size(), bytes.data(), bytes.size());
  if (decompressedSize > 0) {
    std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
    ofs.write((char*)decompressed.data(), decompressedSize);  // Writes decompressed data
  }
  return 0;
}
```

**47. Reading from a LZ4 Archive**

```cpp
#include <iostream>
#include <fstream>
#include <lz4.h>

int main() {
  std::ifstream ifs("data.lz4", std::ios::binary);  // LZ4 archive input stream
  std::vector<uint8_t> bytes(ifs.rdbuf()->in_avail());
  ifs.read((char*)bytes.data(), sizeof(uint8_t) * bytes.size());

  size_t sizeOut = LZ4_decompress_safe(bytes, bytes.data(), bytes.size());
  if (sizeOut > 0) {
    std::ofstream ofs("data.txt", std::ios::binary);  // Decompressed data output stream
    ofs.write((char*)bytes.data(), sizeOut);  // Writes decompressed data

```
