0

I'm trying to open a geotiff from the ASTER data set, but it gives an error that I've been unable to figure out. Here is my code:

#include "stdlib.h"
#include "stdio.h"
#include "tiffio.h"

void read(void);

void main() {
    read();
    return;
}

void read(void) {
    TIFF* file;

    file = TIFFOpen("./ASTGTM2_N50E002_dem.tif", "r");
    if (file != NULL)
        TIFFClose(file);
    else
        printf( "won't open\n" );
    return;
}

I am compiling like so:

gcc parse.c -ltiff -lm;

This is a part of the output I get:

TIFFOpen: ./ASTGTM2_N50E002_dem.tif: Too many open files.
./ASTGTM2_N50E002_dem.tif: Cannot read TIFF header.

The second message repeats a few hundred times, then

won't open

displays a few hundred times after that.

read() is being called once, why am I getting some 700-odd prints?

I'm running Debian, I checked

lsof | grep ASTGTM2_N50E002_dem.tif

and no one has this file open.

I also followed the suggestion here: https://stackoverflow.com/a/9012019/1877851

I am still receiving the same error. What's going on?

Community
  • 1
  • 1
evenex_code
  • 843
  • 1
  • 8
  • 20
  • Run your program like `strace myprog 2>&1 | grep open` and tell us what you find. How many times is open being called? – John Zwinck Mar 07 '13 at 05:01
  • I get some opens on library files, then I get messages like `open("./ASTGTM2_N50E002_dem.tif", O_RDONLY) = n` where 2 – evenex_code Mar 07 '13 at 12:53
  • Is the code above literally your entire program? Have you tried with a different input image? Oh, and by the way, the return type of `main()` must be `int`. – John Zwinck Mar 08 '13 at 02:54
  • Yes, it's the entire program. I'm just trying to open this data and see what I can do with it for now. I've tried different images, same issue. Thanks for the heads up on main(), I looked into it and I understand now. – evenex_code Mar 08 '13 at 03:33
  • I ran your code (after making it say `int main()` and it worked fine. I'm using libtiff 4.0.3 on Mac OS with GCC 4.7.2. I used the example file CCITT_5.TIF from here: http://www.fileformat.info/format/tiff/sample/index.htm – John Zwinck Mar 08 '13 at 03:42
  • I've changed to `int main()` as well, and I tried CCITT_5.TIF, I still get the same error. I'm going to try purging libtiff and reinstalling. – evenex_code Mar 08 '13 at 03:49
  • I had both libtiff5 and libtiff4. I removed libtiff5 and installed libtiff4-dev but the issue remains. – evenex_code Mar 08 '13 at 03:54
  • Try on another platform maybe. Which Linux are you on? – John Zwinck Mar 08 '13 at 03:54
  • Debian, 3.2.0-4-amd64 – evenex_code Mar 08 '13 at 03:56

1 Answers1

3

The problem with your code was staring us right in the face!

You see that function you wrote called read()? Yeah, that's not good. It collides with the standard library's function by the same name (despite different parameters). So it ends up getting called by libtiff--instead of getting data out of the file, it opens recursively, forever until the program can't open files anymore, so libtiff stops trying to read.

Rename your function and all will be well.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436