473,396 Members | 2,111 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,396 software developers and data experts.

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 3939
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

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

Similar topics

4
by: Nicholas Then | last post by:
I am writing an ASP.NET application and I have a class that I have written to create a vCard...it just returns a string with all the necessarry info... Anyway...is there a way that I can create a...
1
by: egsys | last post by:
I am simply trying to create a file in access 2003. Receive an error message that: "Workgroup administrator couldn't create the workgroup information file. Make sure that you have specified a...
4
by: Bnob | last post by:
It is possible to create a file in local user-machine? With this code in code-behind: fs = New System.IO.FileStream(lFileCSV, System.IO.FileMode.Create, System.IO.FileAccess.Write) The file...
11
by: Russ Green | last post by:
I need help writing a method to create a simple text file. I want that file to be saved to a local drive (client side of my asp.net app). The filename will always be the same "license.txt" but the...
1
by: Victor | last post by:
Hi, We have following setup here Web Server (Win2000) and FTP Server (Win 2000) We have ASP.NET application which runs on Web Server and writes file(s) onto FTP Server. We created asp.net...
3
by: christiang | last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting service, you pass an url to its cgi and you get a thumbnail of that url. Of course they don't have the screenshots of all the...
1
by: j7o0s5 | last post by:
How to create file help chm?
0
Kosal
by: Kosal | last post by:
Hi I have problem with vb.net I want to create file setup or exe file for run on Client Computer that no need to install vb.net 2005 but I never do so I don't know what can i do? Please help...
31
by: happyse27 | last post by:
Hi All, How create OOP perl scripts and how to let users select different ouput html template to display the photo that they upload? Including the urls to refer to. Kindly advise... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.