BM Balmiki Mandal 22 Jul 2023 0 views 0 comments C++

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

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!

Share:

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

C++ 14, Features, Programming, Enhancements

Exploring the Features of C++ 14 Programming The release of C++ 14 marks a major milestone in the evolution o...

BM Balmiki Mandal 22 Nov 2023
Understanding Lambda Expressions in C++

Lambdas in C++ Lambda expressions are a powerful feature of C++. They allow you to create anonymous functions...

BM Balmiki Mandal 22 Nov 2023
Create Your Own Pinball Game in C++

Introducing Pinball: The Classic Arcade Game Pinball has been around since the 1930s, and it's one of the mos...

BM Balmiki Mandal 16 Aug 2023