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)						     Linux Programmer's Manual						       TRUNCATE(2)

NAME
truncate, ftruncate - truncate a file to a specified length SYNOPSIS
#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); DESCRIPTION
The truncate and ftruncate functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes. If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as zero bytes. The file pointer is not changed. If the size changed, then the ctime and mtime fields for the file are updated, and suid and sgid mode bits may be cleared. With ftruncate, the file must be open for writing; with truncate, the file must be writable. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
For truncate: EACCES Search permission is denied for a component of the path prefix, or the named file is not writable by the user. EFAULT Path points outside the process's allocated address space. EFBIG The argument length is larger than the maximum file size. (XSI) EINTR A signal was caught during execution. EINVAL The argument length is negative or larger than the maximum file size. EIO An I/O error occurred updating the inode. EISDIR The named file is a directory. ELOOP Too many symbolic links were encountered in translating the pathname. ENAMETOOLONG A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 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. For ftruncate the same errors apply, but instead of things that can be wrong with path, we now have things that can be wrong with fd: EBADF The fd is not a valid descriptor. EBADF or EINVAL The fd is not open for writing. EINVAL The fd does not reference a regular file. CONFORMING TO
4.4BSD, SVr4 (these function calls first appeared in BSD 4.2). POSIX 1003.1-1996 has ftruncate. POSIX 1003.1-2001 also has truncate, as an XSI extension. SVr4 documents additional truncate error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for ftruncate an additional EAGAIN error condition. NOTES
The above description is for XSI-compliant systems. For non-XSI-compliant systems, the POSIX standard allows two behaviours for ftruncate when length exceeds the file length (note that truncate is not specified at all in such an environment): either returning an error, or extending the file. (Most Unices follow the XSI requirement.) SEE ALSO
open(2) 1998-12-21 TRUNCATE(2)
All times are GMT -4. The time now is 11:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy