Archive for October, 2004

Oct
29

Obtaining File Size

Posted by: celso | Comments (0)

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;
}
Categories : Cpp, Notes
Comments (0)
Oct
29

Write To A Text File

Posted by: celso | Comments (0)

This 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;
}
Categories : Cpp, Notes
Comments (0)
Oct
11

Quick Command Line Arg parsing

Posted by: celso | Comments (0)

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.

Categories : Notes, Perl
Comments (0)

Gallery