Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-03-2006, 01:00 AM   #1
Registered: Mar 2005
Posts: 59

Rep: Reputation: 15
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
Old 04-03-2006, 02:08 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,418

Rep: Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398
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.
Old 04-03-2006, 03:23 AM   #3
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
Old 04-03-2006, 09:44 AM   #4
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
truncate(2) truncates a file on disc;
ftruncate(2) truncates a file opened for writing.
Old 04-03-2006, 01:07 PM   #5
Registered: Mar 2005
Posts: 59

Original Poster
Rep: Reputation: 15
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...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating an unresponsive program dangerousdave Linux - Newbie 5 02-16-2005 05:48 PM
Terminating one thread from another orinjus Programming 2 05-20-2004 08:25 PM
programs terminating santasballz Linux - Newbie 3 03-10-2004 10:47 AM
Terminating Remote File Access AceTech747 Linux - Security 15 01-05-2004 11:09 AM
terminating wvdial Freestone Linux - Newbie 6 02-22-2003 01:45 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration