LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   terminating a file (https://www.linuxquestions.org/questions/programming-9/terminating-a-file-431179/)

kryptonite0110 04-03-2006 12:00 AM

terminating a file
 
Does anyone know what function call I need to make to end a file early? For example, if I have a file that is 10 bytes long, and I'm at byte 7, is there a way to end the file now and let the last 3 bytes be ignored?

any help would be greatly appriciated

chrism01 04-03-2006 01:08 AM

Well, ASCII 26 is CTRL-Z aka EOF, but I'm not sure if that would work in this situation.
Personally, I'd read the file into mem, close it, truncate data in mem, then open file for write (thereby clobbering old data) and write data.

cppkid 04-03-2006 02:23 AM

I agree with the "chrism01" that the normal and very typical way to do that is to read the 7 bytes in a buffer and copy to a new file and delete this one.

addy86 04-03-2006 08:44 AM

truncate(2) truncates a file on disc;
ftruncate(2) truncates a file opened for writing.

kryptonite0110 04-03-2006 12:07 PM

thanks for all of the replys. The reason i didn't want to read the data into memory is that file (in this application) would be way to large. I still could have acheved the same effect with a buffer, but i was wondering if there was a wheel already invented. Therefore, I'm going to go with addy86's solution with truncate and ftruncate. Those are actually really useful functions. I'm suprised i'd never heard of them.

Thanks for the help...


All times are GMT -5. The time now is 08:02 AM.