Perl文件读写
Perl File Handling: open, read, write and close files
#====================
Opening files
Solution 1:
Opening a file in perl
open FILE, "filename.txt" or die $!; # read
open FILEHANDLE, MODE, EXPR
The available modes are the following:
| mode | operand | create | truncate |
|---|---|---|---|
| read | < | ||
| write | > | ✓ | ✓ |
| append | >> | ✓ |
Each of the above modes can also be prefixed with the + character to allow for simultaneous reading and writing.
| mode | operand | create | truncate |
|---|---|---|---|
| read/write | +< | ||
| read/write | +> | ✓ | ✓ |
| read/append | +>> | ✓ |
open FILE, ">", "filename.txt" or die $! #write
open FILE, ">filename.txt" or die $!; #write
Solution 2:
#!/usr/bin/perl open(FILE, "<file.txt") or die "Couldn't open file file.txt, $!"; while(<FILE>){
print "$_";
}
Following is the table which gives possible values of different modes
| Entities | Definition |
|---|---|
| < or r | Read Only Access |
| > or w | Creates, Writes, and Truncates |
| >> or a | Writes, Appends, and Creates |
| +< or r+ | Reads and Writes |
| +> or w+ | Reads, Writes, Creates, and Truncates |
| +>> or a+ | Reads, Writes, Appends, and Creates |
Solution 3:
sysopen(FILE, "file.txt", O_RDWR|O_TRUNC );
Following is the table which gives possible values of MODE
| Entities | Definition |
|---|---|
| O_RDWR | Read and Write |
| O_RDONLY | Read Only |
| O_WRONLY | Write Only |
| O_CREAT | Create the file |
| O_APPEND | Append the file |
| O_TRUNC | Truncate the file |
| O_EXCL | Stops if file already exists |
| O_NONBLOCK | Non-Blocking usability |
#====================
Reading files
read a text file line-by-line
my @lines = <FILE>;
while (<FILE>) { print $_; }
while (my $line = <FILE>) { ...}
read a file only a few characters at a time
open FILE, "picture.jpg" or die $!; # read
binmode FILE;
my ($buf, $data, $n);
while (($n = read FILE, $data, 4) != 0)
{ print "$n bytes read\n"; $buf .= $data; }
close(FILE);
#====================
Writing files
open FILE, ">file.txt" or die $!; #write
print FILE $str;
close FILE;
#====================
Closing files
open FILE1, "file.txt" or die $!; # read
open FILE2, "picture.jpg" or die $!; # read
...
close FILE2;
close FILE1;
#====================
REF:
http://www.perlfect.com/articles/perlfile.shtml
Perl文件读写的更多相关文章
- perl5 第五章 文件读写
第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件 语法为open (filevar, file ...
- perl文件操作
Perl 文件操作 Perl 使用一种叫做文件句柄类型的变量来操作文件. 从文件读取或者写入数据需要使用文件句柄. 文件句柄(file handle)是一个I/O连接的名称. Perl提供了三种文件句 ...
- 【Win 10 应用开发】文件读写的三种方案
本文老周就跟伙伴们探讨一下关于文件读写的方法.总得来说嘛,有三种方案可以用,而且每种方案都各有特色,也说不上哪种较好.反正你得记住老祖宗留给我们的大智慧——事无定法,灵活运用者为上. OK,咱们开始吧 ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- Android 文件读写
一.分类 文件读写作为Android四大数据存储方式之一,又分为内部存储和外部存储两种: (1)内部存储(Internal storage): 总是可用. 文件默认情况存储在/data/data/包名 ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化
IO在计算机中指Input/Output,也就是输入和输出. 1.文件读写,1,读文件[使用Python内置函数,open,传入文件名标示符] >>> f = open('/User ...
- [转]Android - 文件读写操作 总结
转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...
随机推荐
- Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- PS4 Razor GPU
这东西,从出来就感觉没用,各种请教也都没有帮助.虽然搞明白了 rt啊tex啊buffer啊但是就是感觉对于抓bug没有用处.所以从来都是像巫师一样靠直觉,再用科学的方法来测试,其实就是让ps retu ...
- 研究Dropbox Server端文件系统
一.传统文件系统 可以理解成两部分:1.真正的storage区,被分割成n个扇区:2.文件系统,其实就是一个FAT表. 二.Dropbox的文件系统 例如,一个modeo.mov的文件,大小为15M. ...
- UML交互图(转载)
概述: 从名字交互作用很明显,图中是用来描述一些不同的模型中的不同元素之间的相互作用.所以,这种相互作用是动态行为的系统的一部分. 这种互动行为表示UML中的两个图,被称为序列图和协作图.这两个图的基 ...
- JAVA float double数据类型保留2位小数点5种方法
/** * Java 两个整数相除保留两位小数,将小数转化为百分数 * java中,当两个整数相除时,由于小数点以后的数字会被截断,运算结果将为整数,此时若希望得到运算结果为浮点数,必须将两整数其一或 ...
- 容器字段FieldContainer
//Ext.form.FieldContainer扩展自Ext.container.Container.当需要把多个字段或组件作为一个表单项展示的时候就需要此组件 Ext.Quick ...
- Javascript 偏移量总结
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- AutoCompleteTextView的应用
现在我们上网几乎都会用百度或者谷歌搜索信息,当我们在输入框里输入一两个字后,就会自动提示我们想要的信息,这种效果在Android 里是如何实现的呢? 事实上,Android 的AutoComplete ...
- 【poj1061-青蛙的约会】拓展欧几里得-不定方程
http://poj.org/problem?id=1061 题意:裸题.注意负数. //poj1061 #include<cstdio> #include<cstdlib> ...
- java应用程序利用Exe4j打包exe文件
1. 使用简介: 把java应用程序打成exe文件我们可以借助第三方软件exe4j来完成.Exe4j大家可以在网上下载,下载地址是: http://www.ej-technologies.c ...