Basic file operations. Searching for string in a file line by line
Basic file operations like open, read, write etc. are essential part of every programming language. In this article we'll make a comparison between implementations in several different languages: C, C++, PHP, Java, Perl, Bash and Python. Bellow are implementation of one and the same task.
We have a file named input.txt. We need to generate file output.txt that contains all lines from input.txt that contain the string "myWord".
Reading and writing text files in C
gcc -o fops.exe file-operations.c && ./fops.exe && cat output.txt
In this example we use fopen for opening both file for reading and writing. If file open operation fails the perror function will print error description. Then we pass the open file descriptors and search term to the function filter_file. It reads form the input line by line using fgets. Then searches for the term with strstr. If there is a match then the whole line is written to the output with fwrite. Note that we don't add new line here since fgets reads the line along with line termination characters.
Reading and writing text files in C++
Note the similarities with the C code above. Let's see what is changed. First we use streams instead of file pointers - ifstream for reading file and ofstream for writing to file. Character buffers are replaced with string objects and thus we now use string::find() for locating matches. Since the C++ getline strips off the line terminators unlike fgets we add extra endl on every output to the result file.
Reading and writing text files in PHP
Here the code is almost the same as in the C implementation. The differences are just that PHP variables are typeless so we don't need to define type.
Reading and writing text files in Java
Let's look in the source of FileFilter class. First its constructor takes the input and output file name along with the search term and initializes member variables. The main logic is in the filter() method. The input is read via BufferedReader.readLine() and the output is written using PrintWriter.println(). Underlying implementations for accessing the actual files are FileReader and FileWriter. Search for the specified term is performed using String.contains().
Reading and writing text files in Perl
Perl version of the above functionality is pretty minimalistic. The main login is in the filter subroutine. First we read three input params using shift. Then open tho handles FPIN for input and FPOUT for output. Note the ">" sign before output file name. This denotes that this file is open for writing. Actual file reading is performed using <FPIN> handle. The index() function returns the first index of a given substring in a string. Note that we don't add new line after each
Reading and writing text files in Bash
Bash provides plenty of tools for filtering file contents, so we can just end up with one line:
grep myString input.txt > output.txt
However to be consistent with the former examples, here's another implementation. First delete any leftover output file and create an empty one. Then use read with redirected input to read each line of the file in the $line variable. Finally match this line against our search term and on success append to the results file.
Reading and writing text files in Python
Here we take the familiar approach. The filter() function does most of the job by opening input and output files and filtering lines of input. When using with construct when opening files, we ensure that this file will be automatically closed when out of scope no matter if exception arises or the block completes successfully. This is a shorthand for try/finally construct. Another notable technique is that a file object can be iterated line by line using for construct.
No comments yet
This page was last modified on 2017-03-28 04:20:53