perl open read file (open)

In Perl, you can use the open or sysopen function to open a file for operation. Both of these functions require a file handle (that is, a file pointer) to read, write, and locate the file.
The following takes the open function as an example:
1: Read: open (file handle, " < file name") / open (file handle, "file name"), the premise file must already exist, otherwise it will return 0, and the error message will be $! middle.
2: Write: open (file handle , "> file name"), if the file does not exist, create it, if it exists, the content is cleared, the length is truncated to 0, and there is an error message in $!
3: Append: open (file handle, " >> file name"), basically the same, but one thing, the content in the file will not be emptied, and the new content will be appended to the original text.
4: Read and write: open (filehandle," +< filename"), via " +<
        (file handle, " +> file name"), through the " + " mode, you can read and write files at the same time, but the difference from the above is that it is a broken write, which will clear the original content.
example:
open(FD,"info.txt")||die("Can not open the file!$!n");
@line=<FD>;
close(FD);
The above example is to open the file info.txt and read the contents of the file into @line, and close the file.
 
Example: (open a file under the 111 file of the computer G drive)
open(IN,"G:/111/mylove.txt");
while($line=<IN>){
        print $line;
}

 

 
from:http://blog.sina.com.cn/s/blog_4af3f0d20100cz8i.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325871029&siteId=291194637