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 saidI've been searching the solution for hours but i can't fine one.It feels like i gonna transform into Super Saiyan.Unhandled exception at 0x0138DC2B in FileInput(Binaries).exe: 0xC0000005: Access violation writing location 0x013931CC.
Here's header for both programs.Here's the fuction code for both programsCode://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(); };
The main.cpp for binary writerCode:#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 readerCode:#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); }
Here's screenchot when I debug the binary reader programCode:#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(); }
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




Reply With Quote
