"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
#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
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.