473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Insert string to file at specific location

I want to open a file and write some string between the the 3rd line and the
4th line. my code (as follows) can insert after the 3rd line but somehow it
owerwrites the 4th line. So i lost its original content. As my original file has
more than 10000 lines of code, I'm looking for a handy way to do that. Does
anyone know how I can fix it? Please help me. Thanks very much.
$content = "new content here\n"
$handle = fopen("test.wrl", "r+"); //open file
$theData = "";
$i = 1;
while($i<4){
$theData += fgets($handle);
$i++;
}
fputs($handle, $content); //write
fclose($handle); //close it
Nov 1 '05 #1
2 4020
Fang wrote:
I want to open a file and write some string between the the 3rd line and the
4th line. my code (as follows) can insert after the 3rd line but somehow it
owerwrites the 4th line. So i lost its original content. As my original file has
more than 10000 lines of code, I'm looking for a handy way to do that. Does
anyone know how I can fix it? Please help me. Thanks very much.
$content = "new content here\n"
$handle = fopen("test.wrl", "r+"); //open file
$theData = "";
$i = 1;
while($i<4){
$theData += fgets($handle);
$i++;
}
fputs($handle, $content); //write
fclose($handle); //close it


Files don't have "lines". They have bytes. Lines are only logical
breaks, defined with \n characters.

To add to the middle of a file, you have to copy the contents of the
file to another one, inserting your text in the appropriate place as you go.

Alternatively, read the entire file into memory (i.e. into an array or a
string), insert your data at the appropriate place and rewrite the file.

The latter sounds easier, but has two problems: if the file gets large,
you'll need lots of memory and perhaps run out of memory. The second is
worse - if you have a problem when rewriting the file (i.e. server
crashes, script times, out, etc.), you may lose the entire file.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 1 '05 #2
I would never rewrite the file like that if there were any danger of
data-loss.

read in 'file.txt'
change in memory
write out 'file.tmp'
delete 'file.txt'
rename 'file.tmp' as 'file.txt'

Nov 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
2
by: help | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
7
by: Larry B via DotNetMonster.com | last post by:
Hello, I seem to be having a problem with inserting data, from a web form, into an access database. I have tried to do the same thing in a regular windows app, there it works fine, but when I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
0
by: teddymeu | last post by:
Hi Guys, since I've done nothing but ask questions these last few weeks to get my first application up and running I thought it was about time to share the wealth and help out a newbie like me since...
6
by: vincent90152900 | last post by:
always get an error message. "Object reference not set to an instance of an object" Can anyone how to solve this? Here is my code. private void CreateNewWordFile(string sourceFileName,string...
6
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.