"C++ how to save data about a object" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "C++ how to save data about a object" answered properly. Developers are finding an appropriate answer about C++ how to save data about a object related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like C++ how to save data about a object. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on C++ how to save data about a object. 

C++ how to save data about a object

By Blue DuckBlue Duck on Feb 26, 2021
#include <fstream>
#include <iostream>

class Something
{
public:
	int weight;
	int size;

	// Insertion operator
	friend std::ostream& operator<<(std::ostream& os, const Something& s)
	{
		// write out individual members of s with an end of line between each one
		os << s.weight << '\n';
		os << s.size;
		return os;
	}

	// Extraction operator
	friend std::istream& operator>>(std::istream& is, Something& s)
	{
		// read in individual members of s
		is >> s.weight >> s.size;
		return is;
	}
};

int main()
{
	Something s1;
	Something s2;

	s1.weight = 4;
	s1.size = 9;

	std::ofstream ofs("saved.txt");

	ofs << s1; // store the object to file
	ofs.close();

	std::ifstream ifs("saved.txt");

	// read the object back in
	if(ifs >> s2)
	{
		// read was successful therefore s2 is valid
		std::cout << s2 << '\n'; // print s2 to console
	}

	return 0;
}

Source: www.cplusplus.com

Add Comment

0

All those coders who are working on the C++ based application and are stuck on C++ how to save data about a object can get a collection of related answers to their query. Programmers need to enter their query on C++ how to save data about a object related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about C++ how to save data about a object for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of C++ how to save data about a object while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "C++ how to save data about a object". Visit this developer's friendly online web community, CodeProZone, and get your queries like C++ how to save data about a object resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "C++ how to save data about a object"

View All C++ queries

C++ queries related to "C++ how to save data about a object"

C++ how to save data about a object how to save data to a file in c++ Dynamically allocate a string object and save the address in the pointer variable p. prevent getting data from data-tooltip-content tippyjs Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set. basic data types in c++ hackerrank solution cpp get data type print data type of a variable in c++ How do you initialize a private static data member in C++? c++ get data type All data types in C++ data types in c++ bst traversal code in data structure with c++ get data from terminal c++ primitive and non primitive data types in c++ c++ asio read full socket data into buffer how to find data size in c++ recursion data structure is not a nonstatic data member or base class of class map data access by key in cpp get type of an object c++ how to initialize an struct object in c++ how to initialize the object in constructor in c++ c++ object program c++ get type name of object cpp loop through object cpp how to create an object of template class calling base class function from derived class object c++ loop trhought object Check whether the jth object is in the subset deifine an object in C++ c++ add object to array cpp create object calling a method on an object c++ remove object from set cpp c++ find object in vector by attribute Remove the jth object from the subset Anonymous Object in C++

Browse Other Code Languages

CodeProZone