08-08-2013 07:44 AM - edited 08-08-2013 07:50 AM
This is a general question about files and file systems ... not necessarily about LabVIEW.
Let's say you have a very large file. The file format doesn't really matter. And you want to append some data to the beginning of the file (i.e. "pre-pend"). Is there any way to do this WITHOUT reading the entire file into memory and then re-writing the entire file? I can't think of a way to do it in LabVIEW.
Is there any way to do it using OTHER programming languages?
EDIT: I understand there are a lot of "workarounds" to this problem. For example, I currently write a bunch of "blank" bytes to the beginning of my file, which I can then go back and overwrite as needed. But my question is more about the ABILITY to do this. I am really curious if there is any feasibnle way to do it using other programming languages ... e.g. could we tell the file system that the "start location" for the file has moved? I suspect that is not possible?
08-08-2013 07:48 AM - edited 08-08-2013 07:51 AM
You need Set File Position function.
So after opening the file reference (using Open/Create/Replace File function) you need to use Set File Position function and then use Write to Text (or Write to Binary) function.
08-08-2013 07:51 AM
I am pretty sure that using "set file position" won't work. That simply sets the offset to where you will write. If you set that to ZERO, it will simply overwrite the bytes at the beginning of the file.
08-08-2013 07:54 AM
@josborne wrote:
I am pretty sure that using "set file position" won't work. That simply sets the offset to where you will write. If you set that to ZERO, it will simply overwrite the bytes at the beginning of the file.
My bad... You're correct..!! It will actually overwrite the file, and you want it to be prepend, that too without reading the file...!!
Is it a text or binary file you're working with??
08-08-2013 07:57 AM
Currently, I am looking at text files. But the question is generic (since all files are binary :-)).
08-08-2013 08:03 AM
This might be done on the operating system level, i.e. you could try to manipulate an FAT (if the file system has one) and create a fragmented file.
Cheers
Edgar
08-08-2013 08:03 AM
@josborne wrote:
Currently, I am looking at text files. But the question is generic (since all files are binary :-)).
Ok, well prepending cannot be done without reading the entire file, I was wondering if Preallocated Read from Binary File function (this can help with memory issues) is of any use.. and my intension of asking the file type is, this function is meant for Binary, but you're right all files are binary at deepest level...!!
08-08-2013 08:06 AM
Hi josborne,
usually you cannot prepend data to a file without copying all the remaining data. (Maybe there exists some exotic filesystem that allows for prepending, but I never have heard of.)
You might think about database usage.
You might think about other fileformats (like tdms) to store properties/information in a structured file.
You might think about using two files instead of just a big one...
08-08-2013 08:09 AM
Another issues is that many file types have format data at the beginning, so prepending ...
08-08-2013 08:20 AM
Thanks everyone. As suspected, it doesn't seem possible.
Strange, though. You'd think the underlying file system would allow this. I understand a bit about file systems. And it doesn't really seem like it should be that hard. In theory, the "pointer" (or whatever it is) that points to the start location of the file would have to change. And if there is no contiguous "empty" space before that location, you'd just have to fragment and pre-pend multiple chunks.
Anyway, this is really a question about file systems (in particularly NTFS), I guess.