C++ I/O Library – Simple and Comprehensive Code Solutions

22 Jul 2023 Balmiki Mandal 0 C++

C++ I/O Library: What It Is and How It Works

The C++ I/O Library is based on the object-oriented model, and provides a variety of classes and functions for reading and writing data to various devices, such as the console, files, and sockets.

Simple Code Solutions:

Printing to the console:

C++
#include <iostream>

int main() {
  std::cout << "Hello, world!" << std::endl;
  return 0;
}

Reading from the console:

C++
#include <iostream>

int main() {
  int number;
  std::cout << "Enter a number: ";
  std::cin >> number;
  std::cout << "You entered the number: " << number << std::endl;
  return 0;
}

Writing to a file:

C++
#include <fstream>

int main() {
  std::ofstream outfile("my_file.txt");
  outfile << "This is a line of text written to a file." << std::endl;
  outfile.close();
  return 0;
}

Reading from a file:

C++
#include <fstream>

int main() {
  std::ifstream infile("my_file.txt");
  std::string line;
  while (std::getline(infile, line)) {
    std::cout << line << std::endl;
  }
  infile.close();
  return 0;
}

Comprehensive Code Solutions:

Formatting output using manipulators:

C++
#include <iostream>
#include <iomanip>

int main() {
  std::cout << std::fixed << std::setprecision(2) << 3.14159 << std::endl;
  // Output: 3.14
  return 0;
}

Reading and writing binary data:

C++
#include <fstream>
#include <vector>

struct Person {
  int age;
  std::string name;
};

int main() {
  std::vector<Person> people;

  std::ifstream infile("people.bin");
  while (infile.read((char*)&people.back(), sizeof(Person))) {
    // Do something with the person data
  }
  infile.close();

  std::ofstream outfile("people.bin");
  for (const Person& person : people) {
    outfile.write((char*)&person, sizeof(Person));
  }
  outfile.close();

  return 0;
}
 

Performing buffered I/O:

C++
#include <fstream>
#include <iostream>

int main() {
  std::ofstream outfile("my_file.txt");
  outfile.imbue(std::locale(std::locale(), new std::cout_syncbuf()));

  for (int i = 0; i < 100000; i++) {
    outfile << i << std::endl;
  }

  return 0;
}
 

Working with multiple streams simultaneously:

C++
#include <fstream>
#include <iostream>

int main() {
  std::ifstream infile("my_file.txt");
  std::ofstream outfile("my_new_file.txt");

  while (std::getline(infile, line)) {
    outfile << line << std::endl;
  }

  infile.close();
  outfile.close();

  return 0;
}

Conclusion:

The C++ I/O Library is a powerful and versatile tool for performing input and output operations. It is easy to use for beginners, but also provides a variety of features for more complex tasks.

Further Reading:

For further information and examples, Please visit[ course in production]

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.