PreciseMotion
PreciseMotion

Reputation: 340

Where to save my c++ program's data in linux file system to be able to access it?

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.

Upvotes: 1

Views: 1526

Answers (1)

Tanktalus
Tanktalus

Reputation: 22254

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.

Upvotes: 4

Related Questions