Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating a file without deleting the entire file

Status
Not open for further replies.

menace212

Programmer
Jul 11, 2003
144
0
0
US
I'm writing a program to manipulate and input file/files...But the problem is when it open the file and writes to it. It deletes the rest of the file leaving what the program put in it. How can I manipulate the program to enter the char/chars in a file without deleting the rest of the file. Ex. C program

EX.

#include<studio.h>
.
.
.
.

main()

{

FILE* f_read;
FILE* f_write;
char* buf[201];

f_write = fopen(&quot;/tmp/file&quot;, &quot;w&quot;);

if (fseek(f_write, 0L, SEEK_SET) < 0) {
perror(&quot;fseek(f_write, 5L, SEEK_SET)&quot;);
}
fputs(&quot;#hello&quot;, f_write);
if (fclose(f_write) == EOF) {
perror(&quot;Error when closing file:&quot;);
}
return 0;
}

before c program
file:

test1
test1
test1
test1
test1

#
__________________________________
after c program
file:

#hello

#
 
&quot;w&quot; mode always truncates the opened file. Use &quot;r+&quot; mode for r/w access to update file. Don't forget use of intervening rewind or fseek (fsetpos) calls when switch between read/write ops.
 
from the code snippet, it seems that you want to update a file. In that case you can also open the file in the append mode.
To open the file using Append Mode, Open the file using a+,a+b or ab+ . This will open file for update at end-of-file or create for update.
You can refer to man page for more details.


 
It's not append mode, it's update mode only snippet. In append mode no need (and impossible) to set write pointer at 0 point (see snippet).
 
Yes ArkM is right.
You need r+ as the file openening mode.
 
Thanks, now if I wanted to move up and down in the file to different points. What method can I use?
 
In the existing code snippet, Open your file with r+ mode. After openeing you can use fseek to move to any place in the file you want.
 
So far have been able manipulate the fseek command to put the char where I want it, but I want to put it at the beginning of the statement without deleting the first char. An example inetd.conf file:


ftp stream tcps nowait .........

My program puts a comment at the beginning but deletes the
first char..

#tp stream tcps nowait...................

Does anyone have any ideas or examples on how I can insert the comment without deleting the first char and create a while loop to insert comments at certain places until the EOF. My sample program is below


#include <string.h>
#include<fcntl.h>
.
.
.
.

main()
{

FILE* f_read
FILE* f_write
FILE* f_readwrite

f_write = fopen(&quot;/tmp/magic1&quot;, &quot;r+)

if (fseek(f_write, 0, SEEK_SET) < 0) {
perror(&quot;fseek(f_write, 0, SEEK_SET)&quot;);
exit(0);
}
fputs(&quot;#&quot;, f_write);

if (fclose(f_write) == EOF) {
perror(&quot;Error when closing file:&quot;)
}
return 0;
}
 
I dont think that you will be able to do that directly with one file without first reading the complete file in RAM and them overwriting the file. The simplest solution can be to read File1 and copy the contents(say line by line) with required changes to File 2(say some temp file). Once you are done with changes(like eof ), close both the files and overwrite File1 with File2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top