05-22-2013 07:27 PM
Hi, all -
I just got a request to save a few program settings, and read those settings on startup. Seems like I need to create a preferences file. (If there's a better way, please advise.)
I know nothing about doing this. Any words of wisdom? My first thought would be XML, though it looks like I'l have to install the device drivers to use it.
1. good idea/bad idea?
2. where do preference files normally go?
3. are preference files generally invisible or somehow rendered uneditable by the user?
Input on this, or anything related, would be appreciated. Thanks!
mz
Solved! Go to Solution.
05-23-2013 12:14 AM
Yes, XML is one possible choice but it's not the only one (and in my opinion not the best one either).
As alternatives I cann suggest:
I personally use an .INI file in each application I write to store configuration and user options and I alway find me confortable with it.
05-23-2013 12:26 AM - edited 05-23-2013 12:40 AM
No words of wisdom , but I am also using ini files. Compared to registry settings I like the fact that in case of problems (say, the program crashed due to a power failure) the file can be edited easily.
For more information, see here and the example ini.cws
05-23-2013 02:20 AM
XML is most modern way of storing application data(eg-preferences) in files in text format and is heavily standardized. But you have to go through a moderate learning curve before you can read and write to and .XML file. But this is worth if you want to store tons of data in neatly organized manner. CVI supports this file standard in form of CVIXML instrument driver typically found in the following path...
C:\Program Files (x86)\National Instruments\CVI2012\toolslib\toolbox\cvixml.fp
Once you loaded this instrument driver, you can start using the functions in them to start reading and writing to XML files (assuming you have basic understanding of XML format).
Storing data in .ini files is also a standard way of storing application initialization/configuration data, but is old fashioned. But is simpler. I donot have much experience with .ini files.
The quickest and easiest way is to write and read from a file is using standard ANSI C functions
fopen(), fprintf(), fscanf() ,fclose(). fprintf and fscanf are very similar to printf() and scanf() functions except that they take filepointer returned by fopen() as additional arguments. These functions should serve your purpose for now as only little data needs to be stored. Note that the preferences file you create is not in standardized format, unlike .ini or .xml.
Preferences file can be stored in same folder as .exe file, so that you do not have to provide full path to access this file. And if you copy your folder containing .exe to somewhere else, you dont have to change your code. I think this is also valid when you have multiple accounts on same computer and application can be launched by any of the users. This is also a good place, if you donot want users to edit preferences file, except in special situtations.
It is possible to make a file hidden or made read-only, but I donot know whether this can be done through CVI. But this is not of much use as users can always unhide it / make it writable. If you really want to prevent its data from being manipulated, either store the data in binary format (fread(), fwrite()) or better encrypt your data.
Regards,
Sudhir
05-23-2013 08:31 AM
Thank you for the detailed replies, everyone. I think I'll take a crack at using .ini files. I can't find anything in the help about them, probably because I don't know the name of what to look for. Can someone point me in the right direction, please?
05-23-2013 08:40 AM
Wolfgang wrote:
For more information, see here and the example ini.cws
Did you follow the link? Searching for Ini in the CVI Help will display this information... Or what else are you looking for?
05-23-2013 08:47 AM
Oops. Sorry, Wolfgang. My middle-aged eyes don't always pick up everything they should. Thanks for that link; that should be enough to get me going.
05-24-2013 02:23 PM
I agree with the others; INI is the way to go. It's easy in LabWindows, it's very readable for your users. It's ASCII. And due to its structure, parsing an INI is really built into the format. In other words, you can very easily lookup values from the file without any elaborate Scan() functions and line parsing.
05-24-2013 03:42 PM
Thanks again for the input, everyone. I've implemented a preferences file that seems to work very nicely, using the .INI approach you all recommended.