04-08-2025 02:29 PM
Hello,
I'm currently trying to implement data overwrites in a file header without reading and replacing the entire file. I've seen suggestions about using the advanced file functions but I'm not exactly sure how to do it. At the moment, I'm using the Read Delimited spreadsheet and Write Delimited spreadsheet VIs but the problem is that I could read the relevant section of the file but writing to file writes the new data but replaces old data that I want to keep. I know I cannot use the spreadsheet VIs for this but I am not sure how to do the implementation with advanced file functions.
I've attached the code screenshot below. I will appreciate all suggestions. Thanks in advance!
04-08-2025 02:43 PM - edited 04-08-2025 02:45 PM
It's going to be tricky to do that directly on the file unless your old and new headers are exactly the same number of characters. Otherwise you risk overwriting data if your new headers have more characters or having junk left if your new headers have less.
I suggest:
04-08-2025 02:55 PM
Using the lowlevel file function you can set the position, then overwrite there. Of course you need to ensure that the old and new headers are exactly the same length. Most likely, you want to reserve sufficient room for the longest reasonable header from the beginning, then pad the new header string with e.g. spaces for exactly that length to fully overwrite that area. If it is too long it will overwrite data and it it is too short, some of the old header could remain.
How big is this file really? Unless it is gigantic, rewriting the entire file is probably a better solution.
04-08-2025 09:29 PM
I'm going to guess that the file of interest has the following two characteristics:
Here's an idea:
Now you have a "repaired" copy. You can do all kinds of things now -- use "Delete" (or "Rename") to get the original file out of the way, and do a "Copy" or "Move" (be sure to read the Help, and pay attention to all the inputs and outputs).
Note -- you may need to look at the Advanced File Functions for some of these things. Experiment (with LabVIEW) and Learn.
Bob Schor
04-09-2025 07:01 AM
Here's some inspiration...
I needed this just a few days ago.
The code replaces the first 3 bytes of a bat file (if they changed), effectively switching between starting an exe at startup or skipping it...
Note that the labels are shown just so you can read them, I normally hide them.
By adding a Set File Position before the Read from Text File, and changing the "0" of the existing Set File Position, you should be able to change data at any position.
The flush is probably not required most of the time, but if you don't flush and LabVIEW exits, files do tend to get corrupted.
04-09-2025 01:11 PM
Hello wiebe@CARYA,
Thanks for your reply. I will give your suggestion a try. The snippet is definitely very helpful.
04-09-2025 01:16 PM
Hi Altenbach,
Thanks for your reply. The file will be accumulating data over time and get very large. That was why I wanted to see if I could avoid having to read the entire file.
04-09-2025 05:40 PM - edited 04-09-2025 05:40 PM
@SolPS wrote:
Hi Altenbach,
Thanks for your reply. The file will be accumulating data over time and get very large. That was why I wanted to see if I could avoid having to read the entire file.
The term "very large" can mean many different things to different people. Formatted text is typically not suitable for very large files. Why not binary instead? You can e.g. reserve the first 1k bytes as "header". You could also use several smaller files.
04-10-2025 05:47 AM
@altenbach wrote:
@SolPS wrote:
Hi Altenbach,
Thanks for your reply. The file will be accumulating data over time and get very large. That was why I wanted to see if I could avoid having to read the entire file.
The term "very large" can mean many different things to different people. Formatted text is typically not suitable for very large files. Why not binary instead? You can e.g. reserve the first 1k bytes as "header". You could also use several smaller files.
Just make sure there's an easy way to get the size of the header.
Even if it's fixed, there might be an update.
If the file has either an easy readable header size or file version, this isn't any problem. Simply overwrite the header with the current version\size or convert the entire file if required.
If there's no header size or file version, this can be a huge problem.
Such a mechanism is hard to add after there are files in the wild...
04-15-2025 04:06 AM
@SolPS wrote:
Hi Altenbach,
Thanks for your reply. The file will be accumulating data over time and get very large. That was why I wanted to see if I could avoid having to read the entire file.
Make sure you have a well defined header so you know what/where you're changing stuff.
E.g.
NameLength U8
Name char[32] i.e. always write 32 characters as name, read the NameLength amount as string, end it with \0 (but always fill out the 32 characters)