1

Okay so I am creating a C++ program, currently on linux. The program has data saved into a file that it needs to access and write to. I'm working on installing my program to /usr/local/bin , but where do files associated / necessary for a program usually get stored?

Putting the text file in /etc doesn't work because then I can't write to the file unless I call my program with sudo permissions. Likewise, /usr/local/share with a directory for my program doesn't work either for the same exact reasons.

What is the typical solution for this or how do people get around this? Where should I be storing the files for my program that it needs to read from and write to? I'm not sure if I'm wording my question incorrectly but just can't really find any insight to this online.

PreciseMotion
  • 330
  • 1
  • 14
  • 3
    Usually that sort of files go in a hidden folder somewhere in the current users home directory. – rdas Apr 05 '19 at 20:42
  • 1
    If per user, under current user's home dir. If global, under /etc/someidorguidorappname – Michael Chourdakis Apr 05 '19 at 20:44
  • 6
    I understand perfectly well how to use linux and am very efficient in the command line. Apparently I'm not allowed to use or code in c++ because I'm not perfectly aware of where to store my associated files... thank you for your meaningless contribution and helping to make the programming community more unwelcoming and less helpful than it already is – PreciseMotion Apr 05 '19 at 20:45
  • Also, thank you very much to the other 2 contributors – PreciseMotion Apr 05 '19 at 20:48
  • 1
    Actually asking such a question means you understand `design` and `convention` importance otherwise you can save your app data anywhere and it works. –  Apr 05 '19 at 20:50
  • There are several places depending on what kind of file it is. Can you add some more context? Is this a config file that stores settings and similar for a program? Is it a file that the user may be working on, like pictures in a photo editor? Is it dependencies the program needs and expects to be able to update on user request, like plugins for an editor? Is it a transient cache for data files that the program generates or downloads, like tracks in a music stream application? – that other guy Apr 05 '19 at 20:54
  • Specifically, if you could explain why this read-write file location has to be hard-coded within the app, instead of being passed as an argument, that would help us in identifying which of the various candidate locations would be good for your use-case. –  Apr 05 '19 at 20:56
  • 2
    `/usr/local/share/myprog/` for data maybe? – Galik Apr 05 '19 at 20:59
  • Possible duplicate of https://stackoverflow.com/questions/29855034/linux-folders-structure-from-developer-perspective – Galik Apr 05 '19 at 21:02
  • Possible useful reading: https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard – Galik Apr 05 '19 at 21:03
  • You have not provided enough information. You need to say more about the data you are reading and writing. Also see [GNU Coding Standards | 7.2.5 Variables for Installation Directories](https://www.gnu.org/prep/standards/html_node/Directory-Variables.html) – jww Apr 05 '19 at 21:04
  • Thanks everyone for your insight. That was precisely the point with Remisa's comment as I was more looking for the common convention for this. To answer Frank, essentially the program is a planner that creates a schedule. All of the command line options need to manipulate and access this file so its essential to the program and that's why its not a command line argument. Hopefully that answers 'that other guy' as well – PreciseMotion Apr 05 '19 at 21:07

1 Answers1

3

You say you're efficient in the command line. So let's say you were writing some shell scripts to do this work instead of C++, where would you store its configuration and data files?

The concepts don't change on language.

If the tool is global, where the user is typically only going to run one per (virtual) machine, you can put your read-only configuration in /etc/<tool>, and your data in /var/lib/<tool>, running as root but dropping privileges as early as possible.

If the tool is per-user, where multiple users may want to use it at the same time with possibly different settings, you can have global configuration in /etc, and user-overrides in ~/.config/<your-app>. And your data would go in ~/.local/<your-app>.

And none of this changes from language to language. What changes is how you piece together the strings to form the paths you want to read, how you read the files, etc., and being cross-platform (the same question for Windows and Mac would give different answers) but that's not what your question entails.

I think you're on the right track, I think there's just some confusion relating to the "C++" part where that doesn't actually change the answer to the question.

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • The only reason I included the C++ part was if I needed to getuid sort of methods in my code or something like that when accessing and manipulating the files. That was the extent of it. I ended up creating a hidden directory under my home directory. – PreciseMotion Apr 05 '19 at 21:03