• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

file closing

i have a file say 1000 bytes.
how can i make the file to be ended at say 900 bytes.
i.e put EOF at 9000 bytes.(in linux)
similarily
how can i produce a file from 100 byte offset to end.
i.e delete first 100 bytes from file.
ANY method other than creating temporary file and writing from 100 to end in it and renaming.
0
sobhan102398
Asked:
sobhan102398
  • 3
  • 2
  • 2
  • +1
1 Solution
 
scrapdogCommented:
1.  

fopen("filename", "a")  opens an existing file (or creates a new one of it doesn't exist), and any data you write to it will be appended to the end of the file.  Open the existing file, write the additional 8000 bytes, the close it and the EOF will be moved.

2.

The only way to do this IS to copy the bytes you want to keep to a new file, rename it, and delete the old file.  Technically, you could try to use BIOS routines to write to the disk directly, but this would take much more code, and an intimate knowledge of the linux disk format and the way the file allocation table is stored.  Copying to another file lets the operating system take care of the details for you, and is less error prone.

 
   
0
 
ozoCommented:
See also truncate or ftruncate
0
 
sobhan102398Author Commented:
a little change in 1st part
sorry i mistyped it.
i have a file say 1000 bytes.
    how can i make the file to be ended at say 900 bytes.
    i.e put EOF at 900 ( not 9000) bytes.(in linux)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kellyjjCommented:
well I suppose you could open the file for both read write, then read until the 900'th char and make the EOF.
0
 
scrapdogCommented:
Yep, that is probably the best way.
0
 
sobhan102398Author Commented:
will putch(EOF,f) works in Linux

0
 
scrapdogCommented:
I don't see why not.
0
 
ozoCommented:
I don't see why it should do anything other than write a '\377' character after the 900th byte
(and even if it did work, it would probably be easier to just seek to the 900th character rather than trying to read up to it)
Why not just use truncate or ftruncate, the functions made for seting a file to a specified length
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now