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文件读写的更多相关文章

  1. perl5 第五章 文件读写

    第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件   语法为open (filevar, file ...

  2. perl文件操作

    Perl 文件操作 Perl 使用一种叫做文件句柄类型的变量来操作文件. 从文件读取或者写入数据需要使用文件句柄. 文件句柄(file handle)是一个I/O连接的名称. Perl提供了三种文件句 ...

  3. 【Win 10 应用开发】文件读写的三种方案

    本文老周就跟伙伴们探讨一下关于文件读写的方法.总得来说嘛,有三种方案可以用,而且每种方案都各有特色,也说不上哪种较好.反正你得记住老祖宗留给我们的大智慧——事无定法,灵活运用者为上. OK,咱们开始吧 ...

  4. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  5. ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...

  6. Android 文件读写

    一.分类 文件读写作为Android四大数据存储方式之一,又分为内部存储和外部存储两种: (1)内部存储(Internal storage): 总是可用. 文件默认情况存储在/data/data/包名 ...

  7. python基础之文件读写

    python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...

  8. 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化

    IO在计算机中指Input/Output,也就是输入和输出. 1.文件读写,1,读文件[使用Python内置函数,open,传入文件名标示符] >>> f = open('/User ...

  9. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

随机推荐

  1. UML对象图(转载)

    概述: 对象图都来源于类图,依赖类图对象图. 对象图表示一个类图的一个实例.类图和对象图的基本概念是相似的.对象图也代表了一个系统的静态视图,但这种静态视图是系统在某一时刻的一个快照. 对象图是用于呈 ...

  2. CSS3弹性盒模型,Flex布局教程

    布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. CSS3中引入flex的弹性盒模型 ...

  3. 如何理解JS项目

    JS API(DOM/PhoneGap/Cordova/NodeJS/Library/Android/MongoDB....)最基础,可以看懂一行代码. -------> JS OOP, JS语 ...

  4. Sqli-labs less 43

    Less-43 本关与42关的原理基本一致,我们还是定位在login.php中的password.看一下sql语句为: $sql = "SELECT * FROM users WHERE u ...

  5. ASP.NET MVC 3和Razor中的@helper 语法

    原文:http://kb.cnblogs.com/page/102191/ ASP.NET MVC 3支持一项名为"Razor"的新视图引擎选项(除了继续支持/加强现有的.aspx ...

  6. .net 类型源码下载地址

    原文:http://www.cnblogs.com/ProJKY/p/SSCLI.html 一般场景下,采用 Reflector可以反射出.NET 的部分实现出来,可以拿来参考,但和微软公开的SSCL ...

  7. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  8. 10个免费开源的JS音乐播放器插件

    点这里 音乐播放器在网页设计中有时候会用到,比如一些时尚类.音乐或影视类等项目,但这些 网页播放器 插件比较少见,所以这里为大家整理一个集合,也许会有用到的时候. 下面整理的播放器有些是支持自适应的, ...

  9. hdu 4061 A Card Game

    思路: 分析:假设取的牌顺序是一个序列,那么这种序列在末尾为1时是和取牌序列一一对应的,且是符合“游戏结束时牌恰好被取完”的一种情况. 简证:1.在序列中,任一数 i 的后一个数 j 是必然要放在第 ...

  10. hdu 4628(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 思路:首先把所有的回文找出来,如果当前状态为回文,则dp[state]=1,否则dp[state ...