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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to text file - no problem, but need each entry on one line!

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
here's my code:

Code:
$cfile = "u/list.txt";
open (COUNTER, "$cfile");
$count = <COUNTER>;
close (COUNTER);
$count = $count.'one line';
open (COUNTER, ">$cfile");
print COUNTER ("$count");
close (COUNTER);

this works great, but i need each entry to be on its own line...any ideas?

it needs to look like this, when opened with a text editor.

entry1
entry2
entry3

Any ideas?

- g
 
You can just print line breaks if you're rewriting the entire file all at once:

Code:
open (WRITE, ">output.txt");
print WRITE "line1\n";
print WRITE "line2\n";
print WRITE "line3";
close (WRITE);

If you're not rewriting the entire file all at once, and your code only writes a single line to the file each time it runs and you want it to write a new line to the file each time, open it in append mode >>

Code:
open (WRITE, ">>output.txt");
print WRITE "my line\n";
close (WRITE);

Use >> instead of > on the file name, and whatever you print to it will be added to the end of what's already there.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
You say it "works great", yet it doesn't do what you're looking for. What are the current contents of list.txt and what exactly are you trying to do?

You should also be aware that if there's a possibility of concurrent access to that file, you'll want to introduce file locking into your script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top