Re: Open file from menu



On Tue, 2012-07-10 at 00:55 +0530, Rudra Banerjee wrote:
Will anyone kindly show the way?


stat(argv[1], &filestat);
buffer = (char *) malloc(filestat.st_size * sizeof (char));
efile = fopen(argv[1], "r");
fread(buffer, filestat.st_size, 1, efile);
gtk_text_buffer_set_text(textbuffer, buffer, filestat.st_size);
free(buffer);

In C you need to check the return status of all system calls.

As Michael already said, first make sure argc > 1;
if a filename was given, make sure that filestat is a struct stat:

struct stat filestat;

Make sure the malloc() successed - if there's not enoug hmemory it'll
return NULL.

Make sure the fopen() succeeded; it too will return NULL on failure.

Strictly speaking you should close the file, too, with fclose(efile),
and check that worked and also check that the free() was OK, if only to
help you catch programming errors!

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml




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