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

What's wrong with this file writing code?

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
What is wrong with the following code?
I wish to write to subdirectory subFiles(created already)'s output.txt file(to be created). There is no output on terminal and no dying log, and worst still no files created either!
Please help!
Code:
use FileHandle;
open (FILE,&quot;+< myFiles\output.txt&quot;) or die &quot;cannot open file for writing&quot;;
    	
$myOutput= &quot;abcd&quot;;
print FILE $myOutput or die &quot;cannot write&quot;;
#Tried without the following three lines as well!
FILE->setvbuf($buffer_var, _IOLBF, 1024);
($readfh, $writefh) = FileHandle::pipe;
autoflush STDOUT 1;

close(FILE);
 
I have modified the file to use
Code:
while(<FILE>)
but now I get a DIE when trying to print to the FileHandle!

How could I create a new file then?

Code:
use FileHandle;
open (FILE,&quot;+< myFiles\output.txt&quot;) or die &quot;cannot open file for writing&quot;;
while(<FILE>)
{        
  $myOutput= &quot;abcd&quot;;
  print FILE $myOutput or die &quot;cannot write&quot;;
  #Tried without the following three lines as well!
  FILE->setvbuf($buffer_var, _IOLBF, 1024);
  ($readfh, $writefh) = FileHandle::pipe;
  autoflush STDOUT 1;
}
close(FILE);
 
There are a couple of things I notice about your open
statement:

1. There should be no space between the mode and the name
of the file.
2. It is probably better to use a forward slash (/)
to separate the directory and file portions. If you
want to use a back slash, you should escape it.

So try this instead:
Code:
open (FILE, &quot;+<myfiles/output.txt&quot;) or die &quot;cannot open file for writing&quot;;
 
I should have read the manual. Spaces between the mode and the filename are OK. I've just never seen it done.
 
Actually, For a start, I just put the While(<FILE>) for experimentation. They are not needed for writing. Anyway, I solved the problem by, guess what :)? getting rid of the space between the mode and the filename!
Also, thoset setbuffer stuff I took from a book is not needed !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top