My program crash when it read my binary file.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 486 Views - Last Post: 07 November 2017 - 08:22 PM Rate Topic: -----

#16 noopofcpp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 17

Re: My program crash when it read my binary file.

Posted 07 November 2017 - 02:00 PM

Next, I don't understand how the code in Post #1 compiles anyway, because the second constructor is declared with a string as its first argument, while the implementation has a char*:
// in Source.h:
Info(string P1name, int P1age, int P1level, int P1hp, int P1Armor);

// in Source.cpp:
 Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)



-----------------
This one
// in Source.cpp:
 Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)

it was a mistake copied char* version with should std::string version .I was trying with char* and std::string but prefer std::string.

View Postr.stiltskin, on 07 November 2017 - 08:50 PM, said:

View Postsnoopy11, on 07 November 2017 - 05:04 AM, said:

However, if you want to use balma,theo or viki initialise them to some empty values before reading anything in.

ie

 Info balma(" ", 0, 0, 0, 0);

//then read the saved data in


First, I don't understand the point of that. Doesn't the OP's default constructor already do this?
Info::Info()
{
	name = "Default";
	age = 0;
	level = 0;
	hp = 0;
	armor = 0;
}


-----------------

Next, I don't understand how the code in Post #1 compiles anyway, because the second constructor is declared with a string as its first argument, while the implementation has a char*:
// in Source.h:
Info(string P1name, int P1age, int P1level, int P1hp, int P1Armor);

// in Source.cpp:
 Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)



-----------------

Next, why are you using a binary file at all, particularly when you are using formatted input >> and output << for the playername (and you're also writing and reading the integer data individually)? If you want to treat it like a text file why don't you simplify matters and use a text file?

-----------------

On the other hand, if you want to use a binary file, replace the std::string with a C-string (char array) and read and write the entire Info object as a single block of data, like this:
//Source.h

#pragma once
#include <fstream>
#include <iostream>
// #include <string>

using namespace std;

class Info
{
private:
    char name[20];  // instead of std::string
    int age;
    int level;
    int hp;
    int armor;
public:
    Info();
    Info(char * const P1name, int P1age, int P1level, int P1hp, int P1Armor);
    void save(ofstream& outfile);
    void load(ifstream& infile);
    void print();
};




#include "Source.h"
// zero-initialize the data members in an initializer list:
Info::Info() : name(), age(), level(), hp(), armor() {}

Info::Info(char* const P1name, int P1age, int P1level, int P1hp, int P1Armor)
{
    // copy the name char by char, or use strcpy (in <cstring>)
    for(int i = 0; i < 20; ++i) {
        name[i] = P1name[i];
        if(P1name[i] == '\0') break;
    }
    age = P1age;
    level = P1level;
    hp = P1hp;
    armor = P1Armor;
}

void Info::save(ofstream &outfile)
{
    // this writes the entire Info object as a single block of binary data
    outfile.write(reinterpret_cast<char*>(this), sizeof(Info));
}
void Info::load(ifstream& infile)
{
    // this reads the entire Info object as a single block of binary data
    infile.read(reinterpret_cast<char*>(this), sizeof(Info));

}

void Info::print()
{

    cout<< "Name  = " << name << endl;
    cout << "Age   = " << age << endl;
    cout << "Level = " << level << endl;
    cout << "HP    = " << hp << endl;
    cout << "Armor = " << armor << endl;
    cout << "--------------------------------------------\n";
}


// in Source.h:
Info(string P1name, int P1age, int P1level, int P1hp, int P1Armor);

// in Source.cpp:
 Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)



-----------------
This one
// in Source.cpp:
 Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)

it was a mistake copied char* version.It should be std::string version .I was trying with char* and std::string but prefer std::string.
[/quote]

Why I use binary it's because i want to learn.I'm not Computer Science student ,I'm a Electrical Enginnering student.I've been learning C++ for a about a month.
Was This Post Helpful? 0
  • +
  • -

#17 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: My program crash when it read my binary file.

Posted 07 November 2017 - 02:54 PM

noopofcpp

Please do not make massive quotes in your post it takes up too much space !
Was This Post Helpful? 0
  • +
  • -

#18 noopofcpp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 17

Re: My program crash when it read my binary file.

Posted 07 November 2017 - 04:36 PM

View Postsnoopy11, on 08 November 2017 - 04:54 AM, said:

noopofcpp

Please do not make massive quotes in your post it takes up too much space !

I'am sorry I'm new in this forum.
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: My program crash when it read my binary file.

Posted 07 November 2017 - 08:22 PM

No need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2