Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)  (Read 2967 times)

0 Members and 1 Guest are viewing this topic.

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
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.
« Last Edit: September 14, 2016, 01:36:26 pm by Christ00pher »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
« Reply #2 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!?
« Last Edit: September 14, 2016, 09:02:03 pm by Christ00pher »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
« Reply #3 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christ00pher

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Cannot open .cfg file (Chapter 4 - 'SFML Game Development by Example' book)
« Reply #4 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!

Abragon

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
I ran into the same problem. Could you show the insert?