Re: Use of fgets, puts, rewind in Gtk+



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, Oct 12, 2006 at 08:14:50PM +0200, Progss wrote:

Let me add some cents to Progss' two dollars :-)

Sucheta Ghosh napisa?(a):
I wished to read a file and get some lines from there like this:
    -----
    fp=fopen("filename", "r");
    for(k=0; k<8; k++){
            n=a[k];
            for (i=1; i<=n; i++) //Here 'n' is the line number
                    fgets(s, 100, fp);
            puts(s);
            rewind(fp);
    }
    -----
    -----
This is the code snippet, which is working so well after compiling with 
'gcc' compiler, but when I added this code inside Gtk+ callback.c, a 
runtime error is occuring before execution of 'fgets'. I have already 
included 'stdio.h', but even then it is not executing.

Do you have any idea that how to overcome this problem?

I can see some problems here, but that's not really GTK-related;
well - there's also a change that included snippet was simply
too short

Exactly. Where are n, a[], s and all of them declared?

1) no error checking for fopen(), fgets() - You don't to anything
    if they fail (and that's most probable)

If fopen() fails, it returns NULL. An fread() or fgets() or whatever
will core dump on that.

2) can't see any fclose() - multiple "fopen()"s without closing
    the file is BAD :)

No crash, but a memory leak. Your program  will accumulate file
descriptors until it runs out of them. This is what they call "file
descriptor leak".

3) You're reading the same file 8 times - and that's *at least*
non-optimal - of course that depends on what exactly you're going
to achieve, however I'm quite convinced

Yes, this looked strange to me as well. But who knows -- may be
something is changing the file (but it must be very quickly indeed). Or
it may be a special file in /proc, yielding different results for each
read pass.

4) I'd be really careful about placing some external file operations
nside of a callback function..... but this depends on your overall
application purpose and design.

Basically I don't see a problem in that -- besides the "feeling" that
file operations are often "taking longer time". And of course, things
that take longer time shouldn't go in a callback, if you don't want your
app to freeze some times.

Regards
- -- tomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFLxRUBcgs9XrR2kYRAif8AJ4q2sbecDywzz8ek/bXsCe5a5ZtAgCfZQRx
lFF8Fg+pixaj6WfLM9Kk4RA=
=4LtO
-----END PGP SIGNATURE-----




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