Perl学习笔记(4)文件的输出输入
来源:互联网 发布:电视型号推荐知乎 编辑:程序博客网 时间:2024/04/29 21:45
1. 文件名前面没有">"表示读文件
open(FILE,"C:\test.txt");
while(<FILE>)
{
chomp;
print "$_\n";
}
close(FILE);
2. 文件名前面有一个">"表示写文件,并覆盖原有内容
open(FILE,">C:\test.txt");
print FILE "大家好\n";
close(FILE);
3. 文件名前面有两个">"表示在这个文件后面追加内容
open(FILE,">>C:\test.txt");
print FILE "大家好\n";
close(FILE);
4. 一次性将文件中的所有内容读入一个数组中(该方法适合小文件):
open(FILE,"filename")||die"can not open the file: $!";
@filelist=<FILE>;
foreach $eachline (@filelist) {
chomp $eachline;
}
close FILE;
@filelist=<FILE>;
当文件很大时,可能会出现"out of memory"错误,这是可以采用如下方法,一次读取一行。
5. 一次从文件中读取一行,一行行地读取和处理(读取大文件时比较方便):
open(FILE,"filename")||die"can not open the file: $!";
while (defined ($eachline =<FILE>)) {
chomp $eachline;
# do what u want here!
}
close FILE;
6. STDIN, STDOUT, STDERR and DATA - Perl file handles
STDIN - Standard Input, for user control input, typically the keyboard
STDOUT - Standard Output,for regular use output, typically the screen
STDERR - Standard Error, for error output; typically defaults to the screen too
例print STDERR "this is an apple.";
DATA - Input for data stored after __END__ at the end of the program
open(FILE,"C:\test.txt");
while(<FILE>)
{
chomp;
print "$_\n";
}
close(FILE);
2. 文件名前面有一个">"表示写文件,并覆盖原有内容
open(FILE,">C:\test.txt");
print FILE "大家好\n";
close(FILE);
3. 文件名前面有两个">"表示在这个文件后面追加内容
open(FILE,">>C:\test.txt");
print FILE "大家好\n";
close(FILE);
4. 一次性将文件中的所有内容读入一个数组中(该方法适合小文件):
open(FILE,"filename")||die"can not open the file: $!";
@filelist=<FILE>;
foreach $eachline (@filelist) {
chomp $eachline;
}
close FILE;
@filelist=<FILE>;
当文件很大时,可能会出现"out of memory"错误,这是可以采用如下方法,一次读取一行。
5. 一次从文件中读取一行,一行行地读取和处理(读取大文件时比较方便):
open(FILE,"filename")||die"can not open the file: $!";
while (defined ($eachline =<FILE>)) {
chomp $eachline;
# do what u want here!
}
close FILE;
6. STDIN, STDOUT, STDERR and DATA - Perl file handles
STDIN - Standard Input, for user control input, typically the keyboard
STDOUT - Standard Output,for regular use output, typically the screen
STDERR - Standard Error, for error output; typically defaults to the screen too
例print STDERR "this is an apple.";
DATA - Input for data stored after __END__ at the end of the program
- Perl学习笔记(4)文件的输出输入
- Perl语言学习笔记 5 输入与输出
- perl的输入与输出
- C++学习笔记:文件输入和输出
- Perl笔记:04、输入与输出
- perl学习日记5输入与输出
- Perl学习之五:输入与输出
- perl学习——输入和输出
- perl学习(5) 输入和输出
- 《C++ primer plus》第十七章 输入、输出和文件(二):文件的输入和输出 学习笔记
- Perl语言入门笔记 第五章 输入与输出(标准输入输出,文件句柄,printf,die,say)
- 《Java的输入/输出》学习笔记
- python学习-文件的输入、输出
- 【C++】学习笔记二十八——文件输入/输出
- Ruby学习笔记(15)_文件输入和输出
- Linux C学习笔记——文件的输入/输出操作
- 【C++ Primer】【学习笔记】【第八章】标准IO库之:文件的输入和输出
- perl学习笔记七----文件
- Qt学习笔记,QWidget和QMainWindow新认识
- How to transport a tablespace from 10g ASM on Windows to 11g ASM on AIX
- 一年的工作,我找到了自己的定位
- Latex(Emacs+Auctex+Jabref)中参考文献处理
- Asp.net 2.0 Treeview 无限级无刷新示例
- Perl学习笔记(4)文件的输出输入
- 模拟提交有文件上传的表单(通过http模拟上传文件)
- 职场中必需修炼的七项意识
- extjs grid 分页 导出 excel
- Eclipse快捷键一览
- 原来+男人+很累的
- Win7Codecs+设置程序中英文对照
- Perl学习笔记(5)glob的使用
- 输入任意个数字存进数组