4 Attachment(s)
[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
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.
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
Attachment 35031
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.
Attachment 35033
Re: My Program Crash When It Read My Binary File.
Code:
outfile << name;
...
infile >> name;
The problem is here. Stream insertion (<<) and stream extraction (>>) are nor the absolute reciprocal of each other. Stream insertion inserts the specified data into the stream (from name), but stream extraction extracts data from the stream (to name) until a white-space character is reached (space, tab or new-line). In this case as when inserting into the stream a terminating white space is not written, stream extraction continues until a white-space char is read or the eof is reached. One way around this is to write a new-line after name but note that stream extraction leaves the found white-space in the stream which will need to be separately extracted. Also note that if name contains a space then only the first part of name (up to the space) will be extracted. Another way is to use getline() to obtain the name if written with a terminating line-feed. This will read the whole of the line into a string (including the terminating line-feed). See http://www.cplusplus.com/reference/s...tring/getline/
Re: My Program Crash When It Read My Binary File.
Thank you 2kaud you saved my problem again it works.
Code:
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));
}