SFML community forums

Help => General => Topic started by: Christ00pher on September 14, 2016, 01:14:13 pm

Title: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: Christ00pher on September 14, 2016, 01:14:13 pm
Hello everybody!

I'm goind through the "SFML Game Development by Example" book and I stuck in chapter 4. The problem is, I can't open 'keys.cfg' file. When I try to do this, I'm getting this message: ! Failed loading keys.cfg.. In fact, the file is called 'keys.cfg.txt', but when I put this name into code, I got an error which says: "Segmentation fault (core dumped)".

I also tried to remove the .txt extension from the file name, but it doesn't work neither. What am I doing wrong? I event copied all files from the resource folder attached to the book, compiled it and the result is the same. I'm working on Ubuntu 16.04, CodeLite, g++.

I'm sure that 'keys.cfg.txt' file is in the right place, because loading of a mushroom texture works perfectly fine, and both files are in the same location.

Opening file:

#include <fstream>
...
std::ifstream bindings;
bindings.open("keys.cfg");
if (!bindings.is_open()){ std::cout << "! Failed loading keys.cfg." << std::endl; return; }


File I'm trying to open is attached.
Title: AW: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: eXpl0it3r on September 14, 2016, 06:00:17 pm
First up decide whether you want to call it keys.cfg.txt or keys.cfg, they are no these same thing.
Next make sure they arw located relative to your working directory.
And if the application segfaults, run it through a debugger and find out why. ;)
Title: Re: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: Christ00pher on September 14, 2016, 08:57:46 pm
Here's the AddBinding function:

bool EventManager::AddBinding(Binding *l_binding){
        if (m_bindings.find(l_binding->m_name) != m_bindings.end())
                return false;

        return m_bindings.emplace(l_binding->m_name, l_binding).second;
}

And here are the lines which crash program:
if (m_bindings.find(l_binding->m_name) != m_bindings.end()) return false;
return m_bindings.emplace(l_binding->m_name, l_binding).second;

They both crash program, I tried to comment the first and the second line, both resulted the same way. Is it something with m_bindings variable?

m_bindings is std::unordered_map<std::string, Binding*> type
Here's the pointer:
struct Binding{
        Binding(const std::string& l_name): m_name(l_name), m_details(l_name), c(0){}
        void BindEvent(EventType l_type, EventInfo l_info = EventInfo()){
                m_events.emplace_back(l_type, l_info);
        }

        Events m_events;
        std::string m_name;
        int c; // Count of events that are "happening".

        EventDetails m_details;
};

So, what's the problem!?
Title: AW: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: eXpl0it3r on September 14, 2016, 09:05:44 pm
Might well be that L_binding is a nullptr and you try to access it -> crash.
Make sure to check your pointer or consider switching to references when the "state" nullptr is not allowed anyways. ;)
Title: Re: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: Christ00pher on September 14, 2016, 11:31:00 pm
You were right, simple if(l_binding != nullptr) made the code work, thank you very much for help!
Title: Re: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
Post by: Abragon on October 21, 2017, 03:53:19 am
I ran into the same problem. Could you show the insert?