Solved

file closing

Posted on 1998-11-16
8
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

691 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