09-19-2008 10:26 AM
Hi!!!
I need to open a binary file, set a position and insert a value.
For example:
1. In my file I have this information:
0 2 4 6 8 10
2. I need to insert
1 3 5 7 9
3. at the end I want this file
0 1 2 3 4 5 6 7 8 9 10
At the moment I have to open the file, read all and update and save it. I'd like to insert the information in the midle. Is it possible? How?
thank you
09-19-2008 11:28 AM
When you mention that you need to insert, what do you mean?
Do you mean that values will be inserted at each next position, like the following example?
1 4 2 7 4 6 1 5 2 3
insert:
9 5 8 2 7 5 9 0 3 2
becomes:
1 9 4 5 2 8 7 2 4 7 6 5 1 9 5 0 2 3 3 2
?
or do they need to be numerically ordered?
like:
0 1 1 2 2 2 2 3 3 4 4 5 5 5 6 7 7 8 9 9 (I might be missing a number, but you should get the point)...
??
09-19-2008 01:25 PM
Hi,
it means that I need to insert in the middle of the file (where you want between the begin and the end of the file). Like the function block "Insert into array"
09-19-2008 01:46 PM
Tricky. If you are just writing scalars like in your examples, I think it is probably most efficient to just write the whole file over again (you don't have to do it in one shot; you can minimize memory usage if the file is really large by doing it in blocks). If you can change the original file to include placeholders (nan's or something), you could replace the nan's with your actual values. That way you wouldn't have to rewrite everything after the change. Another thought, especially if each element is significant in size, is to just append the new data, and then add an index at the end that tells what order the elements actually should be in. Then when you read the file, you just read the index first, and then read the elements back in the order that you want them in. I might do this by writing an integer at the very beginning of the file that contains the offset into the file of the index. The index would just be an array of integers with the offset to each element in the order you want read them. When you add new data, you first read the index, then overwrite the index with the new data, tack a new index at the end, and replace the first integer with the new offset to the index.
I just made this up, but it seems reasonable if the element data is significantly larger than the offset in the index.
Chris
09-20-2008 02:19 PM
Hi!!!
thank you veru much for your advice... I'll use a placeholder
09-23-2008 09:13 AM
Hi Chris,
I have another question for you...
Why I cannot replace a record when I use Datalog file?
09-23-2008 09:41 AM
To tell you the truth, I've been using LabVIEW for 8 years, and I've never used a datalog file. My guess is that datalog files don't have fixed size. The only reason placeholders work in a binary file is that you know you will be replacing an 8 byte item with another 8 byte item. You can do the same thing with clusters of data if every element in the cluster is a fixed size (brown clusters-numerics). It won't work if you include strings or arrays of undefined length.
Maybe someone with some experience with Datalog files can weigh in.
Chris
09-23-2008 01:12 PM
fabio.intel wrote:Why I cannot replace a record when I use Datalog file?
This is well documented in the help.
QUOTE: "Writes record(s) to an open datalog file specified by refnum. Sets the current datalog position to the end of the file before writing."
Elsewhere in the help (section "When to Use Datalog Files")
It says: "You cannot overwrite a record after LabVIEW writes it to a datalog file."
It makes sense that overwriting is not supported or allowed. Imagine the new strings in your record are longer, and instead of single characters now each suddenly contain the collected works of Shakespeare. 🙂 The new record would take up more space and will no longer fit in the same space of the old record..
09-23-2008 01:29 PM - edited 09-23-2008 01:29 PM
You probably know that, but your FOR loops to read all elements from a datalog file are not needed. You can simply do the following: