Skip to content

ReadRecord15C

anna-dodd edited this page Jun 3, 2015 · 1 revision
int main(int argc, char **argv)
{
    nitf_Error error;
    nitf_Reader *reader = null;
    nitf_Record *record = null;
    nitf_IOHandle io = NITF_INVALID_HANDLE_VALUE;
   
    if (argc != 2)
    {
        printf("Usage: %s <nitf-file>\n", argv[0]);
        goto CATCH_ERROR;
    }
   
    /* first, let's check if its even a NITF/NSIF file */
    if (nitf_Reader_getNITFVersion(argv[1]) == NITF_VER_UNKNOWN)
    {
        printf("File: %s is not a NITF\n", argv[1]);
        goto CATCH_ERROR;
    }

    io = nitf_IOHandle_create(fileName, NITF_ACCESS_READONLY,
                              NITF_OPEN_EXISTING, &error);
   
    /* make sure the handle is valid */
    if (NITF_INVALID_HANDLE(io))
        goto CATCH_ERROR;
   
    reader = nitf_Reader_construct(&error);
    if (!reader)
        goto CATCH_ERROR;
   
    record = nitf_Reader_read(reader, io, &error);
    if (!record)
        goto CATCH_ERROR;
   
    /* do something with the record now! */
   
    /* cleanup */
    nitf_IOHandle_close(io);
    nitf_Record_destruct(&record);
    nitf_Reader_destruct(&reader);
   
    return 0;

CATCH_ERROR:
    nitf_Error_print(&error, stdout, "Exiting...");
    if (!NITF_INVALID_HANDLE(io)) nitf_IOHandle_close(io);
    if (record) nitf_Record_destruct(&record);
    if (reader) nitf_Reader_destruct(&reader);
    return EXIT_FAILURE;
}
Clone this wiki locally