LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2005, 05:07 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
insert something in the middle of file using ansi c?


hiho@ll

what is the best way to insert something in the middle of a file using ansi c functions?

it's not really a problem i have, but i just wanted to know the answer, because:
1. FILE *fp=fopen("test.file","a");
fseek(fp,ftell(fp)/2,SEEK_SET);
fprintf(fp,"TEST");

appends TEST at the end of the file (well, it does what it should do)

2. using fopen("test.file","w");
it replaces the text

so, how can i just insert a text using ansi c?
btw: how can this be done in c++?

thx@ll
 
Old 07-25-2005, 05:21 AM   #2
dhirsolo
Member
 
Registered: Apr 2005
Location: India
Distribution: RHEL, Fedora Core , SUSE, Ubuntu, etc
Posts: 54

Rep: Reputation: 15
I think there is no direct function is available which insert a string in middle of file.

Just try it


Open your first file.

Create a new temporary file
write the first part of first file to temporary file (from where you hv to insert the string)

Then write your string to temporary file

then write the rest of the first file to temporary file.

Delete the first file
Rename the temporary file to first file


Or second mathod

Seek to the position where u have to write string.

Copy the rest of the file to another file or memory

append the string at the point you are at.

append the saved piece of file which u saved earlier to memory or temporary file.


Hope it will solve
 
Old 07-25-2005, 05:29 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
You could open a second file, dump the contents of the first file upto the point where you wish to insert the new contents. Then insert the new contents and then write the rest of the file.
 
Old 07-25-2005, 06:13 AM   #4
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
that's what i thought, but i hoped that there is a function which will do the work
because this methods are really unsave i think

thx@ll
 
Old 07-25-2005, 10:31 AM   #5
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
no, there is nothing unsafe about them.
 
Old 07-26-2005, 01:47 PM   #6
dhirsolo
Member
 
Registered: Apr 2005
Location: India
Distribution: RHEL, Fedora Core , SUSE, Ubuntu, etc
Posts: 54

Rep: Reputation: 15
Quote:
Originally posted by Thinking
that's what i thought, but i hoped that there is a function which will do the work
because this methods are really unsave i think

thx@ll
Nothing unsafe in this practice almost every programmer is doing this frequently and found nothing going wrong. Just confidence needed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert date and timestamp Into File name petenyce Linux - Newbie 9 10-13-2005 12:16 PM
daunting task - read wml input, insert variables into URL, DL page, parse, write file jeffreybluml Programming 1 05-12-2005 06:31 AM
Perl: Inserting new data into the middle of a file R00ts Programming 5 03-07-2005 06:48 PM
How can I insert a .GIF file into my post? costasm LQ Suggestions & Feedback 4 10-15-2003 06:04 PM
images to ansi jayakrishnan Linux - General 1 06-06-2002 10:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration