My program crash when it read my binary file.

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 noopofcpp  Icon User is offline

  • New D.I.C Head

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

My program crash when it read my binary file.

Posted 06 November 2017 - 11:20 PM

I made to programs first one is to write binary file and second one is to read it back.The binary file writer works fine but the reader crash.The debugger said

Quote

Unhandled exception at 0x0138DC2B in FileInput(Binaries).exe: 0xC0000005: Access violation writing location 0x013931CC.


I've been searching the solution for hours but i can't fine one.It feels like i gonna transform into Super Saiyan.

Here's header for both programs.
//Source.h

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

using namespace std;

class Info
{
private:
	string name;
	int age;
	int level;
	int hp;
	int armor;
public:
	Info();
	Info(string P1name, int P1age, int P1level, int P1hp, int P1Armor);
	void save(ofstream& outfile);
	void load(ifstream& infile);
	void print();
};

Here's the implementation for both programs
#include "Source.h"

Info::Info()
{
	name = "Default";
	age = 0;
	level = 0;
	hp = 0;
	armor = 0;
}
Info::Info(char* P1name, int P1age, int P1level, int P1hp, int P1Armor)
{
	name = P1name;
	age = P1age;
	level = P1level;
	hp = P1hp;
	armor = P1Armor;
}
void Info::save(ofstream &outfile)
{
	outfile << name;
	outfile.write((char*)&age, sizeof(int));
	outfile.write((char*)&level, sizeof(int));
	outfile.write((char*)&hp, sizeof(int));
	outfile.write((char*)&armor, sizeof(int));
}
void Info::load(ifstream& infile)
{
	infile >> name;
	infile.read(reinterpret_cast<char*>(&age), sizeof(age));
	infile.read(reinterpret_cast<char*>(&level), sizeof(level));
	infile.read(reinterpret_cast<char*>(&hp), sizeof(hp));
	infile.read(reinterpret_cast<char*>(&armor), sizeof(armor));
}
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";
}

The main.cpp for binary writer
#include "Source.h"

int main()
{
	Info balma("Balma", 19, 100, 1500, 1000);
	Info theo("Theo", 19, 95, 1200, 1000);
	Info viki("Viki", 19, 93, 1100, 900);

	ofstream outfile("Player Data.dat",ios_base::binary|ios_base::out);
	balma.save(outfile);
	theo.save(outfile);
	viki.save(outfile);
}

The main.cpp
#include "Source.h"

int main()
{
	Info balma;
	Info theo;
	Info viki;

	cout << "After Loading..." << endl;
	ifstream infile("Player Data.dat",ios_base::binary|ios_base::in);
	if (infile)
	{
		balma.load(infile);
		theo.load(infile);
		viki.load(infile);

		infile.close();
	}
	else if (!infile)
	{
		cout << endl;
		cout << "ERROR 404 Player Data.dat not found" << endl;
		goto quit;
	}
	balma.print();
	theo.print();
	viki.print();
quit:
	cin.get();
	cin.ignore();
}

Here's screenshot of when i debug it.

Is This A Good Question/Topic? 0
  • +

Replies To: My program crash when it read my binary file.

#2 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 06 November 2017 - 11:33 PM

I wanted to upload screenshots but this site said Error The server returned an error during upload
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 06 November 2017 - 11:43 PM

eh ?

You seem to have two int main() routines you can only have one you know ?
Was This Post Helpful? 0
  • +
  • -

#4 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 - 12:02 AM

View Postsnoopy11, on 07 November 2017 - 01:43 PM, said:

eh ?

You seem to have two int main() routines you can only have one you know ?

It's two different program.I've seen my typo but i can't edit it

View Postnoopofcpp, on 07 November 2017 - 02:00 PM, said:

View Postsnoopy11, on 07 November 2017 - 01:43 PM, said:

eh ?

You seem to have two int main() routines you can only have one you know ?

It's two different program.I've seen my typo but i can't edit it

It's two different program the writer and reader
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 12:57 AM

Ah,

Well the problem with your reader program is you don't initialise balma, theo and viki then you try to read information in to an object that is uninitialised.... therefore you get an exception....
Was This Post Helpful? 1
  • +
  • -

#6 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 - 01:31 AM

It seems the problem is not on my code my vs2013 debugger point at a line code of istream header
if (_Ok)
		{	// state okay, extract characters
		const _Ctype& _Ctype_fac = _USE(_Istr.getloc(), _Ctype);

		_TRY_IO_BEGIN
		streamsize _Count = 0 < _Istr.width() ? _Istr.width() : INT_MAX;
		typename _Myis::int_type _Meta = _Istr.rdbuf()->sgetc();
		_Elem _Ch;
		for (; 0 < --_Count; _Meta = _Istr.rdbuf()->snextc())
			if (_Traits::eq_int_type(_Traits::eof(), _Meta))
				{	// end of file, quit
				_State |= ios_base::eofbit;
				break;
				}
			else if (_Ctype_fac.is(_Ctype::space,
				_Ch = _Traits::to_char_type(_Meta))
					|| _Ch == _Elem())
				break;	// whitespace or nul, quit
			else
				*_Str++ = _Traits::to_char_type(_Meta);	// add it to string
		_CATCH_IO_(_Istr)
		}

Right at this code
	*_Str++ = _Traits::to_char_type(_Meta);	// add it to string

Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 01:37 AM

Yes,

I refer you to post #5...

What did I say there about this...

Info balma;
Info theo;
Info viki;



Was This Post Helpful? 2
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 01:37 AM

Yes,

I refer you to post #5...

What did I say there about this...

Info balma;
Info theo;
Info viki;



Was This Post Helpful? 2
  • +
  • -

#9 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 - 01:58 AM

You are right.With this Writer
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int x=10;
	int y=20;
	int z=30;

	ofstream outfile("Coordinate.dat", ios_base::binary);
	outfile.write(reinterpret_cast<char*>(&x), sizeof(x));
	outfile.write(reinterpret_cast<char*>(&y), sizeof(y));
	outfile.write(reinterpret_cast<char*>(&z), sizeof(z));
	outfile.close();
}

and this reader
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int x;
	int y;
	int z;

	ifstream outfile("Coordinate.dat", ios_base::binary);
	outfile.read(reinterpret_cast<char*>(&x), sizeof(x));
	outfile.read(reinterpret_cast<char*>(&y), sizeof(y));
	outfile.read(reinterpret_cast<char*>(&z), sizeof(z));
	outfile.close();
	cout << "Coordinate = " << x << " , " << y << " , " << z << endl;

	cin.get();
	cin.ignore();
}

They work without crash.Thank you very much sir.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 02:04 AM

Ok,

You are Welcome.

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



This post has been edited by snoopy11: 07 November 2017 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#11 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:29 AM

My Output just like this.Is this because of the string.

Quote

Name = Balma d ▄ Φ Theo _ ░ Φ Viki ] L 
Age = 0
Level = 0
HP = 0
Armor = 0
--------------------------------------------
Name = Default
Age = 0
Level = 0
HP = 0
Armor = 0
--------------------------------------------
Name = Default
Age = 0
Level = 0
HP = 0
Armor = 0
--------------------------------------------

Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 03:22 AM

You would need to trim the data like this.

ofstream ofs("Player Data.dat", std::ofstream::out);
    string name = "Fred";
    ofs << name;
    ofs.close();

    string input_name= " ";
    ifstream ifs("Player Data.dat", std::ifstream::in);
    ifs >> input_name;
    ifs.close();
    input_name = trim(input_name);
    cout << input_name << endl;





where the function trim() is given as


std::string trim(const std::string& str,
                 const std::string& whitespace = " \t")
{
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}


Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,387
  • Joined: 27-December 05

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

Posted 07 November 2017 - 06:50 AM

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";
}


This post has been edited by r.stiltskin: 07 November 2017 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

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

Posted 07 November 2017 - 06:54 AM

Oh I missed the constructor ??
Was This Post Helpful? 0
  • +
  • -

#15 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 - 01:43 PM

I found my out solution with is look like this
void Info::save(ofstream &outfile)
{
	outfile << name << endl;
	outfile.write(reinterpret_cast<char*>(&age), sizeof(age));
	outfile.write(reinterpret_cast<char*>(&level), sizeof(level));
	outfile.write(reinterpret_cast<char*>(&hp), sizeof(hp));
	outfile.write(reinterpret_cast<char*>(&armor), sizeof(armor));
}
void Info::load(ifstream& infile)
{
	getline(infile, name);
	infile.read(reinterpret_cast<char*>(&age), sizeof(age));
	infile.read(reinterpret_cast<char*>(&level), sizeof(level));
	infile.read(reinterpret_cast<char*>(&hp), sizeof(hp));
	infile.read(reinterpret_cast<char*>(&armor), sizeof(armor));
}

And r.stiltskin your code works
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()
{


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2