0

I was studying the concepts of file writing. I thought of first compiling the working code and then understanding from the code. But the code in this link does not work. Application crashes if I run. I looked at the input.txt file and I was expecting that to be a text file, but the contents after first execution is "Øûr Micheal Ä Øûr Terry l € "

What could be the issue? Edit: Below is the code:

// C++ program to demonstrate read/write of class
// objects in C++.
#include <iostream>
#include <fstream>
using namespace std;

// Class to define the properties
class Contestant {
public:
    // Instance variables
    string Name;
    int Age, Ratings;

    // Function declaration of input() to input info
    int input();

    // Function declaration of output_highest_rated() to
    // extract info from file Data Base
    int output_highest_rated();
};

// Function definition of input() to input info
int Contestant::input()
{
    // Object to write in file
    ofstream file_obj;

    // Opening file in append mode
    file_obj.open("Input.txt", ios::app);

    // Object of class contestant to input data in file
    Contestant obj;

    // Feeding appropriate data in variables
    string str = "Micheal";
    int age = 18, ratings = 2500;

    // Assigning data into object
    obj.Name = str;
    obj.Age = age;
    obj.Ratings = ratings;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    // Feeding appropriate data in variables
    str = "Terry";
    age = 21;
    ratings = 3200;

    // Assigning data into object
    obj.Name = str;
    obj.Age = age;
    obj.Ratings = ratings;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    return 0;
}

// Function definition of output_highest_rated() to
// extract info from file Data Base
int Contestant::output_highest_rated()
{
    // Object to read from file
    ifstream file_obj;

    // Opening file in input mode
    file_obj.open("Input.txt", ios::in);

    // Object of class contestant to input data in file
    Contestant obj;

    // Reading from file into object "obj"
    file_obj.read((char*)&obj, sizeof(obj));

    // max to store maximum ratings
    int max = 0;

    // Highest_rated stores the name of highest rated contestant
    string Highest_rated;

    // Checking till we have the feed
    while (!file_obj.eof()) {
        // Assigning max ratings
        if (obj.Ratings > max) {
            max = obj.Ratings;
            Highest_rated = obj.Name;
        }

        // Checking further
        file_obj.read((char*)&obj, sizeof(obj));
    }

    // Output is the highest rated contestant
    cout << Highest_rated;
    return 0;
}

// Driver code
int main()
{
    // Creating object of the class
    Contestant object;

    // Inputting the data
    object.input();

    // Extracting the max rated contestant
    object.output_highest_rated();

    return 0;
}
Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • Post code and not links – Ed Heal Nov 07 '17 at 17:36
  • 2
    That code is completely broken. Don't read anything by that author again, they have no idea what they're doing. Get a [real book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – even a cheap, used, outdated one is better than any online tutorial you will find. – molbdnilo Nov 07 '17 at 17:39
  • The code on that website looks like a terrible idea. You can't simply (de)serialize objects that way. E.g. an `std::string` will contain a pointer to `char *`. The code writes that pointer to a file and then reads it back out and tries reconstruct the `std::string` from it. But the memory that this internal `std::string` pointer points to was never allocated, so you'll get a crash at best and undefined behavior otherwise. – AVH Nov 07 '17 at 17:40
  • The file will not be readable because the contents is binary – smac89 Nov 07 '17 at 17:40

1 Answers1

4

The code is fundamentally broken and you should just ignore it.

class Contestant {
public:
    // Instance variables
    string Name;
    int Age, Ratings;

So, Contestant is a class that contains a string.

Contestant obj;

And obj is a Contestant.

// Writing the object's data in file
file_obj.write((char*)&obj, sizeof(obj));

Wait, what? We just write out whatever memory contents the implementation happened to use for obj to a file? That makes no sense at all.

A file is a stream of bytes. To write something to a file, we have to convert it to a comprehensible stream of bytes. This is called "serialization". We can't just take whatever data our implementation happens to use in memory to represent something and write it to a file. What if it contains pointers to other areas of memory?!

The most important thing to understand about files is that they're streams of bytes and that you have to define your data as a stream of bytes to write it to a file. That's what a file format is. Apparently, the author of that code doesn't understand that. The "theory" explained on that page is wrong for this reason as well.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278