Solved

file closing

Posted on 1998-11-16
8
192 Views
Last Modified: 2010-04-15
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
Comment
Question by:sobhan102398
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1254330
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
 
LVL 84

Expert Comment

by:ozo
ID: 1254331
See also truncate or ftruncate
0
 

Author Comment

by:sobhan102398
ID: 1254332
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 2

Expert Comment

by:kellyjj
ID: 1254333
well I suppose you could open the file for both read write, then read until the 900'th char and make the EOF.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254334
Yep, that is probably the best way.
0
 

Author Comment

by:sobhan102398
ID: 1254335
will putch(EOF,f) works in Linux

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254336
I don't see why not.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254337
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question