Sponsored Content
Top Forums Programming how to insert and delete characters in the middle of file Post 56753 by xtrix on Monday 11th of October 2004 03:08:50 AM
Old 10-11-2004
FILE *fp = 0;

const char *path = "/the/path";

fp = fopen(path, "r+");

if (fp)
{
/* move to the appropriate location using fseek() */
/* perforn necessary updates */
fclose(fp);
}
else fprintf(stderr, "Cannot open file %s\n", path);

if u specify the "r+" option the file is opened for update (both reading and writing). u can navigate within the file using the fseek() lib function. this technique allows u to overwrite existing characters located anywhere within a file. i 've used it and it works.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies

2. Shell Programming and Scripting

insert text into the middle of a original file

how do u insert text into a specific place in a file, say the middle for example, without changing the name for that file (1 Reply)
Discussion started by: mopimp
1 Replies

3. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies

4. Shell Programming and Scripting

How to insert the 1st arg into the middle of the file

I wrote a script like #!/bin/bash echo $1 > temp cat $2 >> temp mv temp $2 now I have problem appending the above script(only using bash shell) so that it now inserts the first argument into the middle of the file. I have tried using $(('wc -l < file' / 2 )) but invain so could any one... (4 Replies)
Discussion started by: boris
4 Replies

5. Shell Programming and Scripting

insert one file into middle of another file

how to insert one file into another file not by concatenating as usual done. file1 A B C D E F G H I J K L file2 23455 33444 33334 33345 Output shud be 23455 A B C D (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

insert text in the middle of a file

I want to insert a text into the middle of a file (3 Replies)
Discussion started by: relle
3 Replies

7. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

8. Shell Programming and Scripting

How do I insert a line in the middle of a file in BASH?

I have tried sed '/6/a text_to_inserted' file > newfile but this inserts test_to_insert at random places in file and i want it in specific location, which is line 6. can anyone help.... (6 Replies)
Discussion started by: phenom
6 Replies

9. Shell Programming and Scripting

How to insert text in the middle of a file?

Hi, So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file? Source Code #!/bin/bash #Write... (3 Replies)
Discussion started by: zen10
3 Replies

10. Shell Programming and Scripting

delete blank line from middle of a file

All, I have a file which contains two entry with spaces (either one or more than one space). ex. /tmp/scripts/sql CUST_YR_END_INI.sql /tmp/scripts/sql CUST_WK_END_INI.sql /tmp/scripts/sql CUST_MTH_END_INI.sql /tmp/scripts/sql CUST_YR_END_INC.sql now I want to... (11 Replies)
Discussion started by: anshu ranjan
11 Replies
FOPEN(3)						   BSD Library Functions Manual 						  FOPEN(3)

NAME
fopen, fdopen, freopen, fmemopen -- stream open functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> FILE * fopen(const char * restrict path, const char * restrict mode); FILE * fdopen(int fildes, const char *mode); FILE * freopen(const char *path, const char *mode, FILE *stream); FILE * fmemopen(void *restrict *buf, size_t size, const char * restrict mode); DESCRIPTION
The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it. The argument mode points to a string beginning with one of the following letters: ``r'' Open for reading. The stream is positioned at the beginning of the file. Fail if the file does not exist. ``w'' Open for writing. The stream is positioned at the beginning of the file. Create the file if it does not exist. ``a'' Open for writing. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. Create the file if it does not exist. An optional ``+'' following ``r'', ``w'', or ``a'' opens the file for both reading and writing. An optional ``x'' following ``w'' or ``w+'' causes the fopen() call to fail if the file already exists. An optional ``e'' following the above causes the fopen() call to set the FD_CLOEXEC flag on the underlying file descriptor. The mode string can also include the letter ``b'' after either the ``+'' or the first letter. This is strictly for compatibility with ISO/IEC 9899:1990 (``ISO C90'') and has effect only for fmemopen() ; otherwise the ``b'' is ignored. Any created files will have mode ``S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH'' (0666), as modified by the process' umask value (see umask(2)). Reads and writes may be intermixed on read/write streams in any order, and do not require an intermediate seek as in previous versions of stdio. This is not portable to other systems, however; ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. The fdopen() function associates a stream with the existing file descriptor, fildes. The mode of the stream must be compatible with the mode of the file descriptor. The ``x'' mode option is ignored. If the ``e'' mode option is present, the FD_CLOEXEC flag is set, otherwise it remains unchanged. When the stream is closed via fclose(3), fildes is closed also. The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen() function. If the path argument is NULL, freopen() attempts to re-open the file associated with stream with a new mode. The new mode must be compatible with the mode that the stream was originally opened with: Streams open for reading can only be re-opened for reading, streams open for writ- ing can only be re-opened for writing, and streams open for reading and writing can be re-opened in any mode. The ``x'' mode option is not meaningful in this context. The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout). The fmemopen() function associates the buffer given by the buf and size arguments with a stream. The buf argument is either a null pointer or point to a buffer that is at least size bytes long. If a null pointer is specified as the buf argument, fmemopen() allocates size bytes of memory. This buffer is automatically freed when the stream is closed. Buffers can be opened in text-mode (default) or binary-mode (if ``b'' is present in the second or third position of the mode argument). Buffers opened in text-mode make sure that writes are terminated with a NULL byte, if the last write hasn't filled up the whole buffer. Buffers opened in binary-mode never append a NULL byte. RETURN VALUES
Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. ERRORS
[EINVAL] The mode argument to fopen(), fdopen(), freopen(), or fmemopen() was invalid. The fopen(), fdopen(), freopen() and fmemopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3). The fopen() function may also fail and set errno for any of the errors specified for the routine open(2). The fdopen() function may also fail and set errno for any of the errors specified for the routine fcntl(2). The freopen() function may also fail and set errno for any of the errors specified for the routines open(2), fclose(3) and fflush(3). The fmemopen() function may also fail and set errno if the size argument is 0. SEE ALSO
open(2), fclose(3), fileno(3), fseek(3), funopen(3) STANDARDS
The fopen() and freopen() functions conform to ISO/IEC 9899:1990 (``ISO C90''), with the exception of the ``x'' mode option which conforms to ISO/IEC 9899:2011 (``ISO C11''). The fdopen() function conforms to IEEE Std 1003.1-1988 (``POSIX.1''). The ``e'' mode option does not con- form to any standard but is also supported by glibc. The fmemopen() function conforms to IEEE Std 1003.1-2008 (``POSIX.1''). The ``b'' mode does not conform to any standard but is also supported by glibc. BSD
January 30, 2013 BSD
All times are GMT -4. The time now is 07:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy