04-17-2011 08:25 PM
Hi !
In an ini.files, i have to get the value of each tag named a certain way in a section, but unfortunately, i have 3 or 4 tags having the same name in several sections. I don't know how to do to recover these values, the program always only consider the first tag and not the others, I tried to delete each tag after getting its values it didn't work.
Does someone have an idea to resolve the problem ?
Thanks !
Solved! Go to Solution.
04-17-2011 11:48 PM
As far as I know there is no way to have more than one element in a section with the same name: the instrument simply reads the first one and skips the others.
If you can modify your ini file structure you could for example pack all these values into a single line, something like
Element = "value1; value2; value3"
Or you could differentiate the names of the elements:
Element 1 = value
Element 2 = value
I see no other solution unless you want to abandon the inifile instrument and read the file as an ASCII one scanning lines one by one.
Your attempt to delete the element after reading cannot work since the problem lies in Ini_ReadFromFile function which actually reads file content into the IniText structure in memory: you should save the data to disk with Ini_WriteToFile after that, but doing so you are likely to loose all duplicated elements as they simple aren't in memory!
Well, actually you could try to modify the IniFile instrument, whose code is public, but I personally discourage you from running this way as as the inifile instrument is not a simple code to maintain; additionally you may have to do this work again and again on every CVI update that modifies the instrument.
04-18-2011 12:53 AM - edited 04-18-2011 12:57 AM
Thanks for your answer !
I can't modify the file structure, because it is read by other programs that are not mine.
I think i will read the file like an ASCII file, if I put a pointer on the tag I want the value, is there a function to recover the entire line using that pointer?
04-18-2011 01:39 AM
You could, for example, use the functions in the Formatting and I/O library, SetFilePtr and ReadLine.
04-18-2011 01:48 AM
... or fsetpos and fgets if you prefere to use functions from ANSI C library
04-18-2011 02:32 AM
Finally, this solution is quite complicated. I have already written all the code to select the section i wanted from the file, doing the same thing for the ASCII file would take me more time.
I tried to create a temporary file in which i will copy my Initext and delete the tag once I got its value, but the program bugs and it seems that it always get only the first value, which is supposed to be deleted ?
if (stricmp (modeaffiche, "ENUM") ==0) { Ini_WriteToFile (IniTrames, "c:\\Documents and Settings\\PY\\Desktop\\projet\\scenario\\temp_messageries"); for ( ienumindex=2;ienumindex<=(nbitemsignal-2) ;ienumindex++) { Ini_NthItemName (IniTrames, titre_signal, ienumindex, &plistenum); if ( strcmp(plistenum, "LISTENUM") == 0 ) { IniTemp = Ini_New (0); Ini_ReadFromFile (IniTemp, "c:\\Documents and Settings\\Y\\Desktop\\projet\\scenario\\temp_messageries"); Ini_GetStringIntoBuffer (IniTrames, titre_signal, plistenum, nomenum, 100) ; GetIndexFromValue (panelHandle, PANEL_TREE_SIGNALS, &listenumindex, isignalindex2); ienumvalue= 20000+ienumindex-2; InsertTreeItem (panelHandle, PANEL_TREE_SIGNALS, VAL_CHILD, listenumindex, VAL_LAST, nomenum, "", NULL, ienumvalue); SetTreeItemAttribute (panelHandle, PANEL_TREE_SIGNALS, listenumindex, ATTR_COLLAPSED, 1); Ini_RemoveItem (IniTemp, titre_signal, plistenum); //-----------ecriture dans un fichier temporaire Ini_WriteToFile (IniTemp, "c:\\Documents and Settings\\PY\\Desktop\\projet\\scenario\\temp_messageries"); Ini_Dispose (IniTemp); } }}
04-18-2011 03:29 AM - edited 04-18-2011 03:35 AM
As I already told you, Ini_ReadFromFile skips duplicated items in the file, so once read you have only one element in memory for any given name; calling Ini_WriteToFile will generate a file with that only element written to.
An alternative solution is to use x = Ini_LineOfLastAccess (iniTextHandle); immediately after the Ini_Getxxx function of your interest, saving the line returned from the command; after this you can open the file, read and discard 'x' lines and start reading after that to collect your values. You must be sure that the lines are organized in the proper way for this method to succeed. It is to be noted, additionally, that the connection between each element and the line in the file is calculated during Ini_ReadFromFile process and is not dynamically maintained afterwards: that is, adding or deleting lines from the file has no effect on the value returned by that function.
04-18-2011 04:21 AM
Ok, thank you ! But how can I use the value returned by "Ini_LineOfLastAccess", there is no function to start reading a file from a certain line, right ?
04-18-2011 04:40 AM
As you can read in my last post, you must read and discard 'x' lines:
OpenFile ()
// Skip lines
for (i = 0; i < x; i++) ReadLine ()
// Start reading useful lines
ReadLine () // read a line
... // Interpret the line
Don't forget to add a robust error checking during I/O operations and to check for end-of-file.
04-20-2011 09:54 PM
It works ! 🙂 Thank you so much for your help !