CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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
    Name:  istream error.jpg
Views: 696
Size:  17.9 KB
    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.
    Name:  should be2.jpg
Views: 697
Size:  17.3 KB
    Attached Images Attached Images   
    Last edited by noobofcpp; November 7th, 2017 at 04:15 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2017
    Posts
    50

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

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