[Vala] readline from a FileStream?



Hello,

I've just played around with vala for the first time tonight, and I find it very intriguing! I've been previously using gob for most of my C coding, but I think that I will try to move to vala as much as possible, as soon as I feel comfortable enough with it.

In my efforts to of making sure that I can do simple constructs with the language I tried to read a file line by line. The problem is that fgets() is mapped quite shallowly as follows:

    public weak string gets (string s, int size);

Thus I need to make sure that s has the requested size before calling gets. This is what I eventually came up with:

using GLib;

int main(string[] args) {
    if (args.length < 2) {
        stderr.printf("Need name of file!\n");
        return -1;
    }

    var f = FileStream.open(args[1],"r");
    int n = 1000;
    string s = " ".ndup(n);
    int line = 0;

    while(true) {
        f.gets(s, n);
        if (f.eof())
            break;
        stdout.printf("Line %d: %s", line++, s);
    }
    return 0;
}

Is there a more elegant way than by using ndup(n)?

Thanks again!
Dov



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]