Solved

file closing

Posted on 1998-11-16
8
191 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 102
how to define a string array with big size in c language 10 137
C: GetDiskFreeSpaceEx() for default drive 14 107
Which checksum is this? 7 143
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

773 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