1

I've a problem with creation of C++ project made with CLion. My software should load and save user's profile by disk in order to don't lose own settings every time he opens the program. Obviously it shouldn't be a problem if I knew a pre-defined path for the .txt file but I need that my software always knows where this file stays. Does CLion let creating a sort of txt file that has been built with .cpp files? Is there the possibility of create an "internal memory" for the stand-alone program?

When I run it in CLion I set as "working directory" the project directory and so it knows where to find "data.txt" but when I run the compiled program from terminal (I work on Linux) it doesn't find data.txt. How can I make permanent the "working directory" also for the compiled project? Thank you very much.

A. May
  • 69
  • 4

2 Answers2

3

You have several options.

1) use something like chdir (on POSIX) to always change your CWD (current working directory) to a known location. Then load your data file relative to that directory.

2) store your data in something like QSettings or similar that already knows how to get stuff from a system global database.

3) find the path of your executable (on Linux, read /proc/self/exe - other solutions for doing so exist on other platforms) then read your data from a known location relative to the executable.

4) embed the data in your executable (yes, it is possible to dynamically re-write your executable on disk to store data inside it) then just read the data from within your own executable.

5) store the data at some location the user has to configure and tell you about (like a commandline argument) then read from there.

6) read from a known location relative to the users home directory.

Probably more I didn't think of..

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Note: Your antivirus software will ing HATE option 4. – user4581301 Aug 10 '18 at 18:22
  • 1
    @user4581301 That may be so (provided you are using one), but it *is* an option, so I thought I'd mention it since I was going for "mentioning *all* options"). – Jesper Juhl Aug 10 '18 at 18:25
  • 1
    Yup. That's a warning, not a condemnation. Something to keep an eye out for if the executable keeps mysteriously vanishing or similar. My usual preference is a combination of 6 and 3. My brain's not working right now. Time to log out, I think. – user4581301 Aug 10 '18 at 18:29
  • 1
    @user4581301 what is antivirus? In 2018 I mean? People still use them? – SergeyA Aug 10 '18 at 18:32
  • @SergeyA I have to use it at work. And it routinely deletes my programs because its never seen them before, the program's signature changed after being recompiled, the program does weird stuff with experimental hardware that lies outside the norms for an office desktop, or some unnamed deity just thinks I should have a bad day. It's fun. You should try it. – user4581301 Aug 10 '18 at 18:36
  • Here's how you do option 4: https://balau82.wordpress.com/2012/02/19/linking-a-binary-blob-with-gcc/ – HolyBlackCat Aug 10 '18 at 18:37
  • @user4581301 ever considered spinning up a VM without AV and doing your development in there? Then the host can have all the AV it wants and you don't need to care. – Jesper Juhl Aug 10 '18 at 18:38
  • Several approaches to [Get path of executable](https://stackoverflow.com/questions/1528298/get-path-of-executable). Might be a few new tricks that popped up in ``. Haven't had opportunity to play with it all that much yet. – user4581301 Aug 10 '18 at 18:38
  • Been there, doing that where I can. The real problems start when I copy the program out of the sandbox to test or hand over to another team member or the VM can't talk to the device under test. – user4581301 Aug 10 '18 at 18:41
  • @user4581301 alright, alright. Corporate environments I get. – SergeyA Aug 10 '18 at 18:42
  • @HolyBlackCat nice link. Doesn't tell you how to rewrite the executable on the fly to update the data though. But reading up on ELF or COFF a bit will teach that, so no big deal :) – Jesper Juhl Aug 10 '18 at 18:44
  • 3 and 4 used to work great under MS DOS. Those were the days my friend. As of now these places are in general not writable by regular users. – n. m. could be an AI Aug 10 '18 at 22:19
  • Option number 2 is EXATLY what I need. In fact I'm using Qt library for this project. Thank you very much! – A. May Aug 10 '18 at 23:24
1

Don't re-invent the wheel unless you absolutely, positively have to AND can articulate the problem well enough to document your reasons for starting from scratch.

Your target OS/Distro will have a standard way of saving configuration files on a system-wide and per-user bases. USE THEM! For example, on many Linux distributions you'll find lots of user configurations are saved in the ~/.config directory - all thanks to system libraries that are easy to use and, best of all, fully tested!

Joseph Freivald
  • 396
  • 2
  • 17