Question

I have a visual c++ project named Search Elements in which there is a class called PeriodicTable and I have another visual c++ project called Write Elements in which there is this same class and it's description(variable names and their sizes) is same too...

I use the Write Elements project to write the object of the class Periodic Table to a binary file named PeriodicTable.dat but whenever I use the binary file with my Search Elements project(basically just copy-paste from Write Elements to Search Elements) then the output is unexpected(contains garbage value).

In my opinion the file should work with both the projects as both the projects contains the same class decription. But I don't know what is the problem???

Write Elements Code:

#include <iostream>
#include <fstream>

using namespace std;

class PeriodicTable
{
    char Name[15], Symbol[3], Block, State[10], Colour[15], Classification[20];
    int GroupNo, AtomicNo, PeriodNo;
    float Weight;

    public:

    void GetInfo();
};

int main()
{
    PeriodicTable ptele;
    ofstream fileout;

    fileout.open("PeriodicTable.dat", ios::binary | ios::app);

    system("cls");
    ptele.GetInfo();
    fileout.write((char *)&ptele, sizeof(ptele));

    fileout.close();

    return 0;
}

void PeriodicTable::GetInfo()
{
    cout << "Full Name of the element: ";
    cin >> Name;
    cout << "Symbol: ";
    cin >> Symbol;
    cout << "Block: ";
    cin >> Block;
    cout << "State(at Room Temperature): ";
    cin >> State;
    cout << "Colour: ";
    cin >> Colour;
    cout << "Classification: ";
    cin >> Classification;
    cout << "Group Number: ";
    cin >> GroupNo;
    cout << "Atomic Number: ";
    cin >> AtomicNo;
    cout << "Period Number: ";
    cin >> PeriodNo;
    cout << "Atomic Weight: ";
    cin >> Weight;
}
Was it helpful?

Solution 2

It perfectly works for me with the following read code:

int Read()
{
    ifstream file;
    file.open("PeriodicTable.dat", ios::binary | ios::in);

    while (0 == file.rdstate())
    {
      PeriodicTable ptele;
      file.read((char *)&ptele, sizeof(ptele));
      //if (0 == file.rdstate())
      //    ptele.PrintInfo();
    }
    file.close();

    return 0;
}

OTHER TIPS

You did not perform class abstraction correctly. And more over, everything in same file? including class definition? That's not how you use VS project structure.

I assume you want to create two separate applications. One would create periodic table and a second application use the file generated by first application.

If above is correct, you need to declare the PeriodicTable class in header, implement constructor, destructor and GetInfo method in a .cpp implementation. Another .cpp implementation should contain your main() function for the first application.

Note that it is generally a good idea to create a universal header file in a shared location by two applications which contain global definitions, like the path to the generated .dat file. In this case, absolute path should be taken as both applications will unlikely have same relative path to the file.

Another thing to notice, instead of letting compiler figure out how the file should be structured, structure it yourself. Follow the structure convention you created in both application. One suggestion is that instead of writing (char *) &ptele, write one line for one element, give a tab between each column, say between Element and Symbol. You'll need to parse it properly before using the value but this avoids any ambiguity of the data.

I do not think you use the VS project structure correctly. At the first sight your simple solution should contain one project that would contain one class (PeriodicTable) and this class should have several methods e.g. searchElements, writeElements, readElements etc. What is wrong with this approach? If, for some reason, you cannot use this approach, then I think you should better explain your problem, what you are trying to achieve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top