LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Set File Size Smaller (Truncate?)

I'm running 8.5.

So I'm playing with putting comments in JPEG files, and I've got writing a comment down, and reading the comment back. Now I'm trying to figure out how to edit the comment (I.E. put a smaller or larger comment in).

Due to the format of JPEGs, it's easier to put a bigger comment in. But to put a smaller comment in, basically I need to cut the bottom half of the file off and rewrite it. The only thing that comes after the comment marker in a JPEG file is the End of File marker, so it's not a big idea to replace it.

I'm just wondering if there's an easy way to truncate a file. I tried using Set File Size to set it smaller, and that seems to work. But I'm not sure if that's the intended behavior.

I'd rather not read in the file because you never know how big these things will get and that'll become a memory management problem I think.

Thanks!
0 Kudos
Message 1 of 8
(3,485 Views)
Hi wpolk,
 
I was wondering if you could explain a bit more about your overall application or goal? I'm a bit confused as to if you are just trying to change the font size, style, etc of the comments written to the JPEG files or are you trying to do something with reducing the file size  (Set File Size). Maybe posting a screenshot of your block diagram can help clarify what you are asking. Thanks!
National Instruments
Applications Engineer
0 Kudos
Message 2 of 8
(3,462 Views)
Smiley SadSorry I'm not allowed to post caps or VI's Smiley Sad

Basically I'm trying to figure out how to truncate a file. Just think of the comment as data (It's a cluster at the moment). Since the comment marker is the last marker in a JPEG file, the easiest way to change my comment would be to go to the Comment Marker, cut off everything below it, and just rewrite it and make sure to add the End Of File Marker at the end.

So I'm trying to figure out if Set File Size is a safe way to truncate a file. I know it will truncate a file, but is it supposed to act that way? (I.E. if it's an intended operation then it probably wont be changed in future releases...)
0 Kudos
Message 3 of 8
(3,454 Views)
Well, the Help file for Set File Size says:
Sets the file size of file by setting the end-of-file marker to size bytes from the beginning of the file.
So setting the file size to a value that's smaller than the original size effectively truncates the file. I'm not sure what you mean by "safe way to truncate a file".
0 Kudos
Message 4 of 8
(3,447 Views)
I worry about it not being a safe way to truncate because it's not stated that truncation is expected behavior. Generally there's a specific truncate command in other languages. If it's not intended behavior it could be changed in future releases and cause problems, or not work the way I expect every time.
0 Kudos
Message 5 of 8
(3,441 Views)


wpolk wrote:
I worry about it not being a safe way to truncate because it's not stated that truncation is expected behavior. Generally there's a specific truncate command in other languages. If it's not intended behavior it could be changed in future releases and cause problems, or not work the way I expect every time.


I don't think that is so much a worry about the file system or "other languages."  I think the concern is with the file you are working with.  I don't know the specifications for what makes a valid JPEG file.  If you were working with a text file, knocking off the last byte would do nothing but get rid of the last byte.  But with more complicated file types, losing the last byte might cause the whole file to become unreadable.  It may not just be a one byte type of problem, but some end of file series of bytes acting as a footer so that programs designed to read that file would know how to finish off the read.  I would think .zip files and .pdf files would certainly fall under this category.
0 Kudos
Message 6 of 8
(3,437 Views)
Also, a "truncate" function in one language isn't necessarily the same as another "truncate" in another language. One can interpret "truncate" to mean that you basically open the file and delete all it contents, as the System.IO.FileMode.Truncate enum value does in .NET. Or, you can interpret "truncate" to mean that you set the length, as in the GNU C Library, which makes it work the same way as the LabVIEW Set File Size function.

I think you may be worrying about something that doesn't need worrying over.
0 Kudos
Message 7 of 8
(3,429 Views)


@smercurio_fc wrote:

I think you may be worrying about something that doesn't need worrying over.


You're probably right. Thanks for setting my worries to rest!
0 Kudos
Message 8 of 8
(3,422 Views)