×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

What's wrong with this file writing code?

What's wrong with this file writing code?

What's wrong with this file writing code?

(OP)
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!

use FileHandle;
open (FILE,"+< myFiles\output.txt") or die "cannot open file for writing";
        
$myOutput= "abcd";
print FILE $myOutput or die "cannot write";
#Tried without the following three lines as well!
FILE->setvbuf($buffer_var, _IOLBF, 1024);
($readfh, $writefh) = FileHandle::pipe;
autoflush STDOUT 1;

close(FILE);

RE: What's wrong with this file writing code?

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

How could I create a new file then?


use FileHandle;
open (FILE,"+< myFiles\output.txt") or die "cannot open file for writing";
while(<FILE>)
{        
  $myOutput= "abcd";
  print FILE $myOutput or die "cannot write";
  #Tried without the following three lines as well!
  FILE->setvbuf($buffer_var, _IOLBF, 1024);
  ($readfh, $writefh) = FileHandle::pipe;
  autoflush STDOUT 1;
}
close(FILE);

RE: What's wrong with this file writing code?

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:

open (FILE, "+<myfiles/output.txt") or die "cannot open file for writing";

RE: What's wrong with this file writing code?

I should have read the manual.  Spaces between the mode and the filename are OK.  I've just never seen it done.

RE: What's wrong with this file writing code?

(OP)
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 !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close