Little help for "g_io_channel_seek_position(channel, goffset, G_SEEK_END, NULL);", please?


I need little help here, please.

Actually, this is my first baby step for my process using glib which should work like tail.

Actually, when I first  wrote a process which open a file, and then move the file postion to the end of the file(G_EEK_END), and then call g_io_add_watch_full to "watch" if there are any appended text at the end of the file, I saw that the g_io_add_watch_full callback called constantly.
I confirmed that g_io_add_watch_full works fine with socket. But.. with file descriptor,

FYI - this thread is the one which is about the same issue I have - gnome org/msg10439.html

Anyway, so I had to kept moving back to very basic code to see where the problem was. And the below is the very basic code that I wrote to test the "g_io_channel_seek_position(channel, goffset, G_SEEK_END, NULL);" call.

But even after g_io_channel_seek_position(channel, goffset, G_SEEK_END, NULL) is called, I see that this little program still print out the first line of the file.

If the file position is moved to at the end of file by G_SEEK_END, it shouldn't print out anything, or fail the function call g_io_channel_read_line_string.

But again, it prints out the first line of my test file which is text string file. I have no idea what I'm doing wrong.
Please give me any adivice if anyone has any idea.
Any idea will be helpful.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include <glib.h>

// Kernel, fc6
// cc `pkg-config --cflags --libs glib-2.0` glib_test.c -o glib_test

int main()
        GIOChannel * channel;
        GIOFlags gflag;
        GError *error;
        GIOStatus   status;

        gint64 goffset;
        GString *gbuffer;
        gsize tpos;

        error = NULL;

        channel = g_io_channel_new_file("./file.test", "r", &error );

        if (error) {
                printf("file open error: %s\n", error->message);
                exit (0);

        gflag  = g_io_channel_get_flags(channel);
        status = g_io_channel_set_flags(channel, gflag|G_IO_FLAG_IS_SEEKABLE, &error);

        if ( status != G_IO_STATUS_NORMAL )
                printf("g_io_channel_set_flags failed %d\n", status);

        g_io_channel_seek_position(channel, goffset, G_SEEK_END, NULL);

        gbuffer = g_string_new(NULL);
        g_io_channel_read_line_string(channel, gbuffer, &tpos, NULL);
        g_printf("%s\n", gbuffer->str);

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