469,350 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,350 developers. It's quick & easy.

How i create a file in perl

I have the size of a text file (say 1MB) and i have the location to save the text file (say C:/) , how do i create a file in perl which should be 1MB size and which should save in C:/. Give me some suggestions or ideas. Your help will be appreciated.
Apr 11 '11 #1
11 3634
RonB
589 Expert Mod 512MB
See: `perldoc -f sysopen`
and: `perldoc -f syswrite`
Apr 11 '11 #2
perldoc -f sysopen: Is it a code for creating a file or i need to search on this code..
Apr 11 '11 #3
RonB
589 Expert Mod 512MB
sysopen and syswrite are Perl functions for opening and writing to files.

perldoc is a command that you run from the command line to access the Perl documentation.

You can also access the documentation online.
http://perldoc.perl.org/functions/sysopen.html
http://perldoc.perl.org/functions/syswrite.html
Apr 11 '11 #4
thanku so much RonB!!!! you are wonderfull...
Apr 11 '11 #5
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Fcntl;
  4.  
  5. print "enter the SIZE of a file \n";
  6. $file=<STDIN>;
  7.  
  8. print "enter the location of a file \n";
  9. $loc= <STDIN>;
  10. chomp($loc);
  11.  
  12. sysopen( FILEIN, $loc ,O_CREAT|O_WRONLY);
  13. print FILEIN "hai how r u \n";
  14. close(FILEIN);
  15.  
#In this perl program, im getting the size(1Mb) and location of a file( C:\) from the user. when compiling this
program, the file is saved properly under the location user wants ( C:\) but How to
create a file with the size(1mb) user wants??
Apr 11 '11 #6
RonB
589 Expert Mod 512MB
Use syswrite instead of print.

Please read the documentation that I pointed you to, it will show how how to specify the size.
Apr 11 '11 #7
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Fcntl;
  4.  
  5. print "enter the SIZE of a file \n";
  6. $file=<STDIN>;
  7. chomp($file);
  8.  
  9. print "enter the location of a file \n";
  10. $loc= <STDIN>;
  11. chomp($loc);
  12.  
  13. $new= "hai how are you \n";
  14.  
  15. sysopen( FILEIN, $loc ,O_CREAT|O_WRONLY);
  16. syswrite( FILEIN, $new, $file);
  17. close(FILEIN);
  18.  
#When i compile this program, say im giving the size as 4096, the file is saved properly
under the location it wants but the file size is not equal to user's input. Could you please
tell me what is the error??
Apr 12 '11 #8
RonB
589 Expert Mod 512MB
Currently the size of the file will be the length of the string in $new.

You need add another print statement which adds enough characters to bring the size up to the desired level.
Apr 12 '11 #9
no its not working!!! could you please tell me clearly
Apr 12 '11 #10
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use Fcntl;
  4.  
  5. my $size = 1024;
  6. my $str = 'some random text';
  7. $str .= ' ' x ( $size - length($str) );
  8.  
  9. sysopen( my $fh, 'test.txt' , O_CREAT|O_WRONLY);
  10. syswrite $fh, $str;
  11.  
Apr 12 '11 #11
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Fcntl;
  4.  
  5. $new= "hai how are you \n";
  6.  
  7. print "enter the SIZE of a file in BYTES\n";
  8. $file=<STDIN>;
  9. chomp($file);
  10.  
  11. print "enter the location of a file \n";
  12. $loc= <STDIN>;
  13. chomp($loc);
  14.  
  15. $new .= ' ' x ( $file- length($new));
  16.  
  17. sysopen( FILEIN, $loc , O_CREAT|O_WRONLY);
  18. syswrite( FILEIN, $new);
  19. close(FILEIN);
  20.  
#This perl program is working perfectly. RonB, you are amazing.
But last question, could you please explain,

Expand|Select|Wrap|Line Numbers
  1. $new .= ' ' x ( $file- length($new));
  2.  
what this line does??
Apr 13 '11 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Nicholas Then | last post: by
1 post views Thread by egsys | last post: by
4 posts views Thread by Bnob | last post: by
11 posts views Thread by Russ Green | last post: by
1 post views Thread by Victor | last post: by
3 posts views Thread by christiang | last post: by
1 post views Thread by j7o0s5 | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.