LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-03-2006, 12:00 AM   #1
kryptonite0110
Member
 
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, 01:08 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 02:23 AM   #3
cppkid
Member
 
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, 08:44 AM   #4
addy86
Member
 
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, 12:07 PM   #5
kryptonite0110
Member
 
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...
 
  


Reply


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 04:48 PM
Terminating one thread from another orinjus Programming 2 05-20-2004 07:25 PM
programs terminating santasballz Linux - Newbie 3 03-10-2004 09:47 AM
Terminating Remote File Access AceTech747 Linux - Security 15 01-05-2004 10:09 AM
terminating wvdial Freestone Linux - Newbie 6 02-22-2003 12:45 PM


All times are GMT -5. The time now is 10:25 AM.

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