Solved

file closing

Posted on 1998-11-16
8
190 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
Comment Utility
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
Comment Utility
See also truncate or ftruncate
0
 

Author Comment

by:sobhan102398
Comment Utility
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
 
LVL 2

Expert Comment

by:kellyjj
Comment Utility
well I suppose you could open the file for both read write, then read until the 900'th char and make the EOF.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
Yep, that is probably the best way.
0
 

Author Comment

by:sobhan102398
Comment Utility
will putch(EOF,f) works in Linux

0
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
I don't see why not.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now