Obtaining File Size

October 29, 2004

The 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;
}

Write To A Text File

October 29, 2004

This snippet will write a text file.


#include ;

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;
}

Quick Command Line Arg parsing

October 11, 2004

To 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.

Bad Behavior has blocked 21 access attempts in the last 7 days.