CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Oct 2017
    Posts
    50

    [RESOLVED] My Program Crash When It Read My Binary File.

    I made two 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
    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.
    Code:
    //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 fuction code for both programs
    Code:
    #include "Source.h"
    
    Info::Info()
    {
    	name = "Default";
    	age = 0;
    	level = 0;
    	hp = 0;
    	armor = 0;
    }
    Info::Info(string 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
    Code:
    #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 for binary reader
    Code:
    #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 screenchot when I debug the binary reader program
    istream error.jpg
    And it should be like this.This is the version that read data from a plain text file genearted by my text writter version program.
    should be2.jpg
    Attached Images Attached Images
    Last edited by noobofcpp; November 7th, 2017 at 04:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured