Opening a file for Read/Write/AppendYou can open a file using the function open(). It takes three arguments: a file handle which is just a symbolic name of your choice for the file, a mode symbol which specifies the file operation mode in which the file should be opened, and third being the file to be opened itself.
Mode operators are as given below.< : Read - Default if a mode is not specified at all.
> : Write -File is Created if not existing else Overwrites.
>> : Append - creates file if not existing.
If you want to combine read and write, prepend a "+" to the modes.
+< : Read/Write,Don't create file,Overwrites existing.
+> : Read/Write,Create file if not existing, else Overwrite.
+>> : Read/Append,Create file if not existing, no Overwrite
Let us take an example.
open ( MY_FILE_HANDLE,"<","/home/safeer/docs/accounts.txt");This will open the file "
/home/safeer/docs/accounts.txt" for reading and will be accessible through the file handle
MY_FILE_HANDLE. We can use this handle to read data from the actual file.
The second and third arguments for open() can be combined into a single string as "
< /home/safeer/docs/accounts.txt". In the case of mode, it is OK even if you skip the "<" symbol - since read is the default mode. So,
open ( MY_FILE_HANDLE,"<","/home/safeer/docs/accounts.txt");open ( MY_FILE_HANDLE,"< /home/safeer/docs/accounts.txt");open ( MY_FILE_HANDLE,"/home/safeer/docs/accounts.txt");are all the same.
If you want to open this file for writing, use
open ( MY_FILE_HANDLE,">","/home/safeer/docs/accounts.txt"); ORopen ( MY_FILE_HANDLE,"> /home/safeer/docs/accounts.txt");Reading data from a fileOpen the file in read modeopen ( MY_FILE_HANDLE,"< /home/safeer/docs/accounts.txt");You can assign the the entire file content to an array variable where each element of the array will correspond to a single line in the file.
@acounts_data = <MY_FILE_HANDLE> ;But this can be memory intensive if the file you are opening is very large. A better way is to read file line by line and process it as follows.
while ( $line = <MY_FILE_HANDLE> ){#Process $line here}In every iteration $line will contain a single line of the file. Note that the last character of the $line will be newline (\n), so you may want to chomp $line before processing it. The following code will be equivalent to printing the file contents.
while ( $line = <MY_FILE_HANDLE> ){chomp ($line);print"$line\n";}Write/Append to a file:We use print command to write/append (depends on the mode in which the file is opened) to a file.
Syntax:
print $FILEHANDLE $STRING;To start writing, first open file in write mode and then print using the file handle obtained from open().
open ( MY_FILE_HANDLE,"> /home/safeer/docs/newaccounts.txt");$my_account = "safeer:/home/safeer/:usr/bin/bash" ;print MY_FILE_HANDLE "$my_account\n";Alternatively, write directly without using variable:
print MY_FILE_HANDLE "safeer:/home/safeer/:usr/bin/bash\n" ;If the file was opened in append mode ( ">>" ) the string would have been appended to the end of file.
open ( MY_FILE_HANDLE,">> /home/safeer/docs/accounts.txt");print MY_FILE_HANDLE "safeer:/home/safeer/:usr/bin/bash\n" ;Closing the fileany file that is opened should be closed once the purpose of open is over.
close (MY_FILE_HANDLE);