LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Overwriting a portion of a text file without reading the entire file

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!Overwrite text file portion.png

0 Kudos
Message 1 of 14
(369 Views)

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:

  1. Read the entire file into a 2D String array using "Read Delimited Spreadsheet"
  2. Replace the first line of the array (the headers) with the new headers
  3. Write the entire array back to the file using "Write Delimited Spreadsheet"

 


========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 2 of 14
(362 Views)

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.

0 Kudos
Message 3 of 14
(354 Views)

I'm going to guess that the file of interest has the following two characteristics:

  1. It's a text file, "human-readable" (meaning letters and numbers, not unlike this reply I'm typing here), specifically with "readable characters" divided into "lines" (which are probably "invisible" characters called "Carriage Return" (hex 0x0D) and "Line Feed" (hex 0x0A).  
  2. The file is shorter than half the the free space on your hard drive (or, perhaps, the free space on a USB Stick you plug into a USB port on your PC).

Here's an idea:

  1. Open the file for Reading (only).
  2. Open another file for Writing (and give it a different name -- if it's on a different drive, say a "D:" drive, you can use the same file name as "D:myfile.csv" is different from "C:\Users\SolPS\Documents\myfile.csv".
  3. Read a line from the "Source".  If it is OK, write it (a line) to the "Copy".  If it is simply to be eliminated,  don't do anything with the "Copy", but if it is to be replaced, replace it with an alternate line (or lines) written to "Copy".
  4. Continue until you've made an "altered Copy" by replacing selected lines.  Close both files.

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

  1.  
0 Kudos
Message 4 of 14
(308 Views)

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...

Replace 3 bytes.png

 

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.

0 Kudos
Message 5 of 14
(280 Views)

Hello wiebe@CARYA,

Thanks for your reply. I will give your suggestion a try. The snippet is definitely very helpful.

0 Kudos
Message 6 of 14
(245 Views)

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. 

0 Kudos
Message 7 of 14
(241 Views)

@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.

0 Kudos
Message 8 of 14
(215 Views)

@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...

0 Kudos
Message 9 of 14
(191 Views)

@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)

 

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 14
(129 Views)