Sponsored Content
Top Forums Programming How to delete N bytes from the starting of the file from a C function??? Post 302145927 by porter on Thursday 15th of November 2007 09:25:06 PM
Old 11-15-2007
No, there is no function to delete from the middle of a file, only to truncate.

You have two options

(a) open one file, read buffer fulls and write earlier in the file until you get to the end then truncate

(b) open a new file, copy the data you want to keep to the new file and then close it and rename it onto the original.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

delete bytes from file

I'm doing a bit of hex editing with dd and I can replace values fairly simply. However, I've run across a situation where I need to delete bytes in the file and I'm not sure how to do that. For example: Input file has: 1234567890 Output needs to be: 123abc90 I tried this: printf... (6 Replies)
Discussion started by: Loriel
6 Replies

2. Shell Programming and Scripting

Delete lines starting with XX or YY or ZZ or ....

Hi There! My final task for today is to delete lines starting with certain numbers for e.g., my text block is and i want to delete all lines starting with 11 or 17 or 21 I know i can use multiple sed commands like sed '/^11,/d' <filename> sed '/^17,/d' <filename> sed '/^21,/d'... (2 Replies)
Discussion started by: orno
2 Replies

3. Shell Programming and Scripting

How to delete initial N bytes from the starting of the file using sed?

I want to delete initial N bytes of a file using sed. How can I do that? (3 Replies)
Discussion started by: jockey007
3 Replies

4. Shell Programming and Scripting

Remove first N bytes and last N bytes from a binary file on AIX.

Hi all, Does anybody know or guide me on how to remove the first N bytes and the last N bytes from a binary file? Is there any AWK or SED or any command that I can use to achieve this? Your help is greatly appreciated!! Best Regards, Naveen. (1 Reply)
Discussion started by: naveendronavall
1 Replies

5. Shell Programming and Scripting

Delete a line in a file starting with - Perl one-liner

Hi I need a perl onliner to delete a line in a file starting with few words. Example file.txt ---------- my name is don I live in London I am woking as engineer I want to delete a line starting with 'I live in' using perl oneliner and in place edit with out temporary files Thanks... (2 Replies)
Discussion started by: ammu
2 Replies

6. Programming

Copying 1024 bytes data in 3-bytes chunk

Hi, If I want to copy a 1024 byte data stream in to the target location in 3-bytes chunk, I guess I can use the following script. dd bs=1024 count=3 if=/src of=/dest But, I would like to know, how to do it via a C program. I have tried this with memcpy(), that did not help. (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. UNIX for Dummies Questions & Answers

X bytes of 0, Y bytes of random data, Z bytes of 5, T bytes of 1. ??

Hello guys. I really hope someone will help me with this one.. So, I have to write this script who: - creates a file home/student/vmdisk of 10 mb - formats that file to ext3 - mounts that partition to /mnt/partition - creates a file /mnt/partition/data. In this file, there will... (1 Reply)
Discussion started by: razolo13
1 Replies

8. UNIX for Advanced & Expert Users

How to delete a* file with out deleting file name starting with a?

Hi, I have a query: I have a bunch of files starting letter with 'a' (example: abhi,all,anand,animal,a1.txt,allow.java,a*) here i want to delete/remove only a* folder but not other files and folders. and a* folder is present in so many other folders. what is unix command to delete... (9 Replies)
Discussion started by: rajanikanth86
9 Replies

9. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

10. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies
TRUNCATE(2)						      BSD System Calls Manual						       TRUNCATE(2)

NAME
ftruncate, truncate -- truncate or extend a file to a specified length SYNOPSIS
#include <unistd.h> int ftruncate(int fildes, off_t length); int truncate(const char *path, off_t length); DESCRIPTION
ftruncate() and truncate() cause the file named by path, or referenced by fildes, to be truncated (or extended) to length bytes in size. If the file size exceeds length, any extra data is discarded. If the file size is smaller than length, the file is extended and filled with zeros to the indicated length. The ftruncate() form requires the file to be open for writing. Note: ftruncate() and truncate() do not modify the current file offset for any open file descriptions associated with the file. RETURN VALUES
A value of 0 is returned if the call succeeds. If the call fails a -1 is returned, and the global variable errno specifies the error. ERRORS
The ftruncate() system call will fail if: [EBADF] fildes is not a valid descriptor open for writing. [EFBIG] The file is a regular file and length is greater than the offset maximum established in the open file description associ- ated with fildes. [EINVAL] fildes references a socket, not a file. [EINVAL] fildes is not open for writing. [EROFS] The named file resides on a read-only file system. The truncate() system call will fail if: [EACCES] Search permission is denied for a component of the path prefix. [EACCES] The named file is not writable by the user. [EFAULT] Path points outside the process's allocated address space. [EISDIR] The named file is a directory. [ELOOP] Too many symbolic links are encountered in translating the pathname. This is taken to be indicative of a looping symbolic link. [ENAMETOOLONG] A component of a pathname exceeds {NAME_MAX} characters, or an entire path name exceeds {PATH_MAX} characters. [ENOENT] The named file does not exist. [ENOTDIR] A component of the path prefix is not a directory. [EROFS] The named file resides on a read-only file system. [ETXTBSY] The file is a pure procedure (shared text) file that is being executed. The ftruncate() and truncate() system calls will fail if: [EFBIG] The length argument was greater than the maximum file size. [EINTR] A signal is caught during execution. [EINVAL] The length argument is less than 0. [EIO] An I/O error occurred while reading from or writing to a file system. SEE ALSO
open(2) BUGS
These calls should be generalized to allow ranges of bytes in a file to be discarded. Use of truncate() to extend a file is not portable. HISTORY
The truncate() and ftruncate() function calls appeared in 4.2BSD. 4.2 Berkeley Distribution June 4, 1993 4.2 Berkeley Distribution
All times are GMT -4. The time now is 09:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy