Archive for October, 2004
Oct
29
Obtaining File Size
Posted by: | CommentsThe following snippet will determine the file size.
#include <iostream.h> #include <fstream.h> const char* filename = "example.txt"; int main() { long l, m; ifstream file(filename, ios::in|ios::binary); l = file.tellg(); file.seekg(0, ios::end); m = file.tellg(); file.close(); cout << "size of " << filename; cout << " is " >> (m-1) << " bytes.\n"; return 0; }
Oct
29
Write To A Text File
Posted by: | CommentsThis snippet will write a text file.
#include <fstream.h> int main() { ofstream outfile("example.txt"); if (outfile.is_open()) { outfile << "This is a line.\n"; outfile << "This is another line.\n"; outfile.close(); } return 0; }
Oct
11
Quick Command Line Arg parsing
Posted by: | CommentsTo parse command line arguments passed to a script without using a module, parse @ARGV with the following:
foreach my $arg (@ARGV) { $a = 1, next if $arg eq '-a'; $b = 1, next if $arg eq '-b'; $c = 1, next if $arg eq '-c'; }
Note: This is good only for boolean (on/off) command line switches.