10-16-2013 03:26 PM
Hi,
So I'm having trouble understanding a Set File Size VI. I have a text file set up for a limit of 10000 bytes that will continually record system faults (User defined faults). This should then record a time stamp, the fault description, and then write the file to the text file. The next fault to follow will do the same thing on the line underneath. This file, however, is expanding well beyond the specified file size, in some cases 2 Gigabytes!
What I would like to happen is if the file reaches the limit, restart from the beginning and overwrite previous faults or flush the file. I'm not sure why its going over the file size if its set up at the beginning. If anyone could shed light on why its still expanding I'd really appreciate it!
10-16-2013 04:17 PM
you may be using this function incorrectly?
this example 'gets file size' and compares it to the size I want(100 bytes), then bools to the append file terminal of 'write to spreadsheet'...as long as my file is less than, append information . otherwise new file if more than
10-16-2013 04:38 PM
Hello T._P_Tithering,
The Set File Size function does not limit the size of the file it is directed to- rather it moves the EOF character to the position specified. If used on a file larger than the size specified, it will truncate the file. Otherwise, the function will increase the size of the file to match what you've specified.
Checking the file size and deleting or replacing it periodically is probably what you're going to need to do - are you keeping the file in memory at all times or loading/writing only when needed? If you're loading it periodically, just check the size ahead of time and use the "replace" rather than "open" with the Open/Replace/Create function.
Regards,
10-16-2013 05:20 PM
Thank you for the quick reply! I'd like to implement it that way but I feel like I've misunderstood the concept of that Set File Size. I would have thought that the file would stop growing once it hits that size.
10-17-2013 07:46 AM
Hi Tom,
Thanks for clearing that up. I will be writing to the file only in a error situation. If I use the replace function will that flush out the data and start recording the data as if I opened a new file?
10-17-2013 08:30 AM
Hi guys,
I used the code apok sent to perform the action I needed. It will work for this application but I have a feeling it can be improved. Here is my code. Thanks again for clearing it up my questions. Feel free to comment.
10-17-2013 11:17 AM
Hello T._P_Tithering,
To answer your questions in order:
1) The Set File Size function sets the file size when it is used, but does not affect the maximum file size. As far as I know, there is no automatic way to limit file sizes other than the limitations caused by the OS's file system.
2) If a file is replaced, then yes- the previous file will be written over and the new data will replace whatever was there previously.
3) Taking a look at your VI, it looks like you've modified Apok's logic that determines whether or not the new data is appended to the file or replaces it. This isn't necessary- in fact, by removing the connection to the Write to Spreadsheet File's Append to File? Input, it looks like you'll be overwriting/replacing the file each time it is written to (this is the default operation of the function). I don't think this is what you want. I would take a look at the detailed help for the function (right-click>>help) - this describes how this input works and how it affects your write operation.
You're also erasing the original data contained in the array within your "true" case, but I'm not exactly sure what the purpose of this shift register/array accumulator is- I suspect it's just demonstrative in Apok's code, and I don't believe you'll need it. It looks like what's actually happening in your VI is that the file is being erased/replaced each time it is written to, but you are limiting the size of the array being written to the file to only the latest ~100 bytes of data.
I'd also double-check the bytes logic (looks like it maxes out at 100 rather than the 10000 you specified), and I think there is some work to be done on how this write operation will be triggered -how will you get your error data into the loop? Where is it coming from in your application? Will this function be in a subVI? Will the subVI be running all of the time in the background waiting for errors, or will it be called as needed?
Hope that helps!
Regards,
10-17-2013 11:47 AM
my example was a quick/dirty one at that....from the code, i am storing the data onto the SR, that may not be a good idea as the array can get quit large.maybe when the array gets to a certain size, take that chunk of data and write it to the file at that point, instead of rewriting the file on every iteration of the loop as I have shown which is not a good idea. so with bool logic, write/append to file when new data is recieved individually or in chunks and replace file on ce it has reached the set limit size...
10-17-2013 03:39 PM - edited 10-17-2013 03:41 PM
ok...try this?