Read excel file in C using libxls - c

I am trying to create an application that reads an excel file using libxls.
I've already downloaded libxls but I do not know how to use it.
Can someone please show me a minimum program to read from a XLS file

If you mean libxls:
There are tests inside the source archive which can be used as sample code.
Here's the content of libxls-1.4.0/test/test2.c:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* This file is part of libxls -- A multiplatform, C/C++ library
* for parsing Excel(TM) files.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2004 Komarov Valery
* Copyright 2006 Christophe Leitienne
* Copyright 2008-2012 David Hoerl
*
*/
// THIS FILE LETS YOU QUICKLY FEED A .xls FILE FOR PARSING
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libxls/xls.h"
int main(int argc, char *argv[])
{
xlsWorkBook* pWB;
xlsWorkSheet* pWS;
unsigned int i;
if(argc != 2) {
printf("Need file arg\n");
exit(0);
}
struct st_row_data* row;
WORD t,tt;
pWB=xls_open(argv[1],"UTF-8");
if (pWB!=NULL)
{
for (i=0;i<pWB->sheets.count;i++)
printf("Sheet N%i (%s) pos %i\n",i,pWB->sheets.sheet[i].name,pWB->sheets.sheet[i].filepos);
pWS=xls_getWorkSheet(pWB,0);
xls_parseWorkSheet(pWS);
printf("pWS->rows.lastrow %d\n", pWS->rows.lastrow);
for (t=0;t<=pWS->rows.lastrow;t++)
{
printf("DO IT");
row=&pWS->rows.row[t];
xls_showROW(row);
for (tt=0;tt<=pWS->rows.lastcol;tt++)
{
xls_showCell(&row->cells.cell[tt]);
}
}
printf("Count of rows: %i\n",pWS->rows.lastrow);
printf("Max col: %i\n",pWS->rows.lastcol);
printf("Count of sheets: %i\n",pWB->sheets.count);
xls_showBookInfo(pWB);
} else {
printf("pWB == NULL\n");
}
return 0;
}
If you mean libxl:
Below you'll find a data reading example written in C from http://www.libxl.com/.
Furthermore they provide documentation and more examples on their homepage.
BookHandle book = xlCreateBook();
if(book)
{
if(xlBookLoad(book, L"example.xls"))
{
SheetHandle sheet = xlBookGetSheet(book, 0);
if(sheet)
{
double d;
const wchar_t* s = xlSheetReadStr(sheet, 2, 1, NULL);
if(s) wprintf(L"%s\n", s);
d = xlSheetReadNum(sheet, 3, 1, NULL);
printf("%g\n", d);
}
}
xlBookRelease(book);
}

Related

Looking for Informix4GL by Example header file 'fgicfunc.h'

I am working through the Informix4GL by Example cookbook. Exercise 13 deals with creating external callable modules written in C. The example code contains this line:
#include "fgicfunc.h"
But I cannot find this file or a description of its contents. It is supposed to be related to the Informix database software product but I do not have this installed. Does anyone here have that file; and if you do then can you provide me the contents?
Despite the copyright notice, etc, this is published when you install the I4GL p-code compiler.
/**************************************************************************/
/* */
/* Licensed Materials - Property of IBM */
/* */
/* "Restricted Materials of IBM" */
/* */
/* IBM Informix 4GL */
/* (c) Copyright IBM Corporation 2010 All rights reserved. */
/* */
/**************************************************************************/
/***************************************************************************
*
* INFORMIX SOFTWARE, INC.
*
* PROPRIETARY DATA
*
* THIS DOCUMENT CONTAINS TRADE SECRET DATA WHICH IS THE PROPERTY OF
* INFORMIX SOFTWARE, INC. THIS DOCUMENT IS SUBMITTED TO RECIPIENT IN
* CONFIDENCE. INFORMATION CONTAINED HEREIN MAY NOT BE USED, COPIED OR
* DISCLOSED IN WHOLE OR IN PART EXCEPT AS PERMITTED BY WRITTEN AGREEMENT
* SIGNED BY AN OFFICER OF INFORMIX SOFTWARE, INC.
*
* THIS MATERIAL IS ALSO COPYRIGHTED AS AN UNPUBLISHED WORK UNDER
* SECTIONS 104 AND 408 OF TITLE 17 OF THE UNITED STATES CODE.
* UNAUTHORIZED USE, COPYING OR OTHER REPRODUCTION IS PROHIBITED BY LAW.
*
*
* Title: fgicfunc.h
* Description: 4GL C function header file
*
***************************************************************************
*/
#ifndef IBM_I4GL_FGICFUNC_H
#define IBM_I4GL_FGICFUNC_H
typedef struct
{
char *cf_name; /* name of function */
int (*cf_ptr)(int); /* pointer to the function */
short cf_nargs; /* number of arguments, < 0 means variable */
} cfunc_t;
#endif /* IBM_I4GL_FGICFUNC_H */
We can discuss the erratic boxing comments another time.

How to improve Mersenne Twister vor AVX/SSE?

Today i have started a project having the goal to optimize the generation of random numbers.
I want to wipe several hard drives, using the Mersenne Twister PRNG, but unfortunately i'm only able to produce around 200MB/s of random data, on 8 hard drives, so 25MB/s each.
Is there a way to optimize this code, using AVX, or SSE (for legacy reasons) to improve this code?
Is it something what can be done without having studied computer science?
Unfortunately i'm a simple C programmer, only having the experience of a few years, but not having studied computer science yet.
Could someone provide some information, how to bring this forward?
Which of these processes could be improved? Can someone give some examples, how an enhanced version of it could look like? Can someone provide some good books to retrieve a better knowledge about this matter?
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Copyright (C) 2005, Mutsuo Saito,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat # math.sci.hiroshima-u.ac.jp (remove space)
*/
#include <stdio.h>
#include "mt19937ar.h"
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s)
{
mt[0]= s & 0xffffffffUL;
for (mti=1; mti<N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt[mti] &= 0xffffffffUL;
/* for >32 bit machines */
}
}
/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++, 2004/2/26 */
void init_by_array(unsigned long init_key[], int key_length)
{
int i, j, k;
init_genrand(19650218UL);
i=1; j=0;
k = (N>key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++; j++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
if (j>=key_length) j=0;
}
for (k=N-1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}
/* generates a random number on [0,0xffffffff]-interval */
unsigned long genrand_int32(void)
{
unsigned long y;
static unsigned long mag01[2]={0x0UL, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) { /* generate N words at one time */
int kk;
if (mti == N+1) /* if init_genrand() has not been called, */
init_genrand(5489UL); /* a default initial seed is used */
for (kk=0;kk<N-M;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (;kk<N-1;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
mti = 0;
}
y = mt[mti++];
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}
I think, Mersenne Twister PRNG is not suitable for your purpose, because of it is not cryptographically secure. If cryptographer can guess or recover piece of MT sequence (for example, from initially 0-filled disk part), he can recover your generator state, and recover entire your sequence, by re-run PRNG from that state.
I suggest you to use modification of RC4 PRNG, but works with array of "uint16_t [1 << 16]", rather than original byte-oriented RC4 with array "uint8_t [1 << 8]". This modification will give you 2 benefits:
For each computation step, you will extract 2 bytes, not one, as in the original. By other words, you will get ~2x performance improvement, this is important for you.
Period of this generator would be extremely long, I estimate it as 2^(2^14).
To preserve attack to your KSA (major RC4 vulnerability), I suggest you to "empty intial run" 2^18 times before usage. By other words, you initially, just read and drop initial 2^18 uint16_t values from PRNG.
Also, you can modify the KSA, to make it more hack-resistant.

Multiple SPI masters active at once

I'm facing the problem when trying to get 2 SPI masters working at same time. They both have different pins(mosi, miso, sck, ss) and one is used by SD-Card and another one accelerometer. My device is nRF52832 and I have been trying to use SPI drivers from nRF52 SDK 15.2.0.
I'm not sure if they can be initialized and in use same time, but its feeling quite wrong if the program needs to init and uninit another spi instance whenever it needs to communicate with another one.
Seems that whenever one instance gets initialized the second one does not work correctly. The program does not give any error. This is my first touch to embedded systems and because of that, my skill level is the novice. I really would appreciate all the help.
/**
* Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** #file
* #defgroup fatfs_example_main main.c
* #{
* #ingroup fatfs_example
* #brief FATFS Example Application main file.
*
* This file contains the source code for a sample application using FAT filesystem and SD card library.
*
*/
#include "nrf.h"
#include "bsp.h"
#include "ff.h"
#include "diskio_blkdev.h"
#include "nrf_block_dev_sdc.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_drv_spi.h"
#define FILE_NAME "NORDIC.TXT"
#define TEST_STRING "SD card example."
#define SDC_SCK_PIN 16 ///< SDC serial clock (SCK) pin.
#define SDC_MOSI_PIN 17 ///< SDC serial data in (DI) pin.
#define SDC_MISO_PIN 18 ///< SDC serial data out (DO) pin.
#define SDC_CS_PIN 11 ///< SDC chip select (CS) pin.
#define SDC2_SCK_PIN 5
#define SDC2_MOSI_PIN 4
#define SDC2_MISO_PIN 3
#define SDC2_CS_PIN 12
#define SPI_AC_INSTANCE 1
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_AC_INSTANCE);
/**
* #brief SDC block device definition
* */
NRF_BLOCK_DEV_SDC_DEFINE(
m_block_dev_sdc,
NRF_BLOCK_DEV_SDC_CONFIG(
SDC_SECTOR_SIZE,
APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
),
NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
);
/**
* #brief Function for demonstrating FAFTS usage.
*/
static void fatfs_example()
{
static FATFS fs;
static DIR dir;
static FILINFO fno;
static FIL file;
uint32_t bytes_written;
FRESULT ff_result;
DSTATUS disk_state = STA_NOINIT;
// Initialize FATFS disk I/O interface by providing the block device.
static diskio_blkdev_t drives[] =
{
DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
};
diskio_blockdev_register(drives, ARRAY_SIZE(drives));
NRF_LOG_INFO("Initializing disk 0 (SDC)...");
for (uint32_t retries = 3; retries && disk_state; --retries)
{
disk_state = disk_initialize(0);
}
if (disk_state)
{
NRF_LOG_INFO("Disk initialization failed.");
return;
}
uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;
NRF_LOG_INFO("Capacity: %d MB", capacity);
NRF_LOG_INFO("Mounting volume...");
ff_result = f_mount(&fs, "", 1);
if (ff_result)
{
NRF_LOG_INFO("Mount failed.");
return;
}
NRF_LOG_INFO("\r\n Listing directory: /");
ff_result = f_opendir(&dir, "/");
if (ff_result)
{
NRF_LOG_INFO("Directory listing failed!");
return;
}
do
{
ff_result = f_readdir(&dir, &fno);
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Directory read failed.");
return;
}
if (fno.fname[0])
{
if (fno.fattrib & AM_DIR)
{
NRF_LOG_RAW_INFO(" <DIR> %s",(uint32_t)fno.fname);
}
else
{
NRF_LOG_RAW_INFO("%9lu %s", fno.fsize, (uint32_t)fno.fname);
}
}
}
while (fno.fname[0]);
NRF_LOG_RAW_INFO("");
NRF_LOG_INFO("Writing to file " FILE_NAME "...");
ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
return;
}
ff_result = f_write(&file, TEST_STRING, sizeof(TEST_STRING) - 1, (UINT *) &bytes_written);
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Write failed\r\n.");
}
else
{
NRF_LOG_INFO("%d bytes written.", bytes_written);
}
(void) f_close(&file);
return;
}
static uint8_t tx_buf[3];
static uint8_t rx_buf[10]; /**< RX buffer. */
uint8_t accReadByte(uint8_t RegisterAddress)
{
tx_buf[0] = 0x7F & RegisterAddress;
tx_buf[1] = 0x80 & RegisterAddress;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, tx_buf, 2, rx_buf, 1+2));
return rx_buf[2];
}
void initAcc()
{
//nrf_gpio_set_cfg_output(SDC2_CS_PIN);
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SDC2_CS_PIN;
spi_config.miso_pin = SDC2_MISO_PIN;
spi_config.mosi_pin = SDC2_MOSI_PIN;
spi_config.sck_pin = SDC2_SCK_PIN;
spi_config.frequency = NRF_SPI_FREQ_250K;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
}
/**
* #brief Function for main application entry.
*/
int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("FATFS example started.");
fatfs_example();
initAcc();
if ( accReadByte(0x0D) == 0x6A )
{
NRF_LOG_INFO("Got right whoami!");
}
while (true)
{
__WFE();
}
}
/** #} */
Problem solved. SD-Card and the accelerometer both have different serial bus in default. When initialized another one first, second one failed in bus initialization and that caused nRF52832 not to get the right answer.

best way to create a string array through macro

I am trying to crate a parametric static array in a header file.
Let me elaborate my requirement
Wanted to create below array in header file statically which can be resolved at
compile time
static char test_array[][256] = {
"hello_1",
"hello_2",
"hello_3" };
I need to generate this using macro which can do substitution with %d for 1,2 and 3 and the number
of parameter also not fixed. What I mean here is tomorrow this array I can simply change through header file to
static char test_array[][256] = {
"hello_1",
"hello_45",
"hello_39",
"hello_101" };
I have the code to do it at run time in .c file but specifically I want to do it in a header file through macros .
The best way would be to write generator, that generates header array in compile time. A short time ago I wrote program that may point you the right direction:
/*
* Copyright (c) 2018 Krzysztof "Palaiologos" Szewczyk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define PERLINE 30
#define BUFSIZE 1024
static const char hexrange[] = "0123456789ABCDEF";
char buf[BUFSIZE];
int lineidx,line,i,bytes;
int main(int argc, const char * argv[]) {
FILE * in, * out;
if (argc != 4) {
fprintf(stderr, "Usage: %s infile outfile symbol\n", argv[0]);
return 1;
}
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
if (!in || !out) {
fputs("Could not open one of input files.\n", stderr);
return 1;
}
fprintf(out, "/* Generated by HexExport (by Krzysztof Szewczyk a.k.a. Palaiologos) */\n\n");
fprintf(out, "static const unsigned char %s[] = {", argv[3]);
while ((bytes = fread(buf, 1, sizeof(buf), in)) > 0) {
for (i = 0; i < bytes; ++i) {
int byte = ((uint8_t *)buf) [i];
if (lineidx++ == 0) {
if (line++)
fputc(',', out);
fputs("\n\t", out);
} else
fputs(", ", out);
fputs("0x", out);
fputc(hexrange[byte >> 4], out);
fputc(hexrange[byte & 0xF], out);
if (lineidx >= PERLINE)
lineidx = 0;
}
}
fputs("\n};\n", out);
fclose(in);
fclose(out);
return 0;
}
What it does, simply, is to generate hex array from file. You can modify it to accomplish your task. Generating such array using macro is either impossible or very hard task. Source code taken from this place

hdf5 example code h5_rdwt.c does not work

I have a problem running an example code from hdf5 called h5_rdwt.c (in eclipse). You can find this here:
http://www.hdfgroup.org/HDF5/Tutor/rdwt.html#rdwr
The code is:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help#hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to write and read data in an existing
* dataset. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "dset.h5"
int main() {
hid_t file_id, dataset_id; /* identifiers */
herr_t status;
int i, j, dset_data[4][6];
/* Initialize the dataset. */
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
dset_data[i][j] = i * 6 + j + 1;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open an existing dataset. */
dataset_id = H5Dopen2(file_id, "/dset", H5P_DEFAULT);
/* Write the dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset_data);
status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset_data);
/* Close the dataset. */
status = H5Dclose(dataset_id);
/* Close the file. */
status = H5Fclose(file_id);
}
Before I did so, I created a file named "dset.h5" with:
#include "hdf5.h"
#define FILE "dset.h5"
main() {
hid_t file_id; /* file identifier */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Terminate access to the file. */
status = H5Fclose(file_id);
}
Building is no problem, but when I try to run this I get the message:
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: ../../src/H5D.c line 334 in H5Dopen2(): not found
major: Dataset
minor: Object not found
#001: ../../src/H5Gloc.c line 430 in H5G_loc_find(): can't find object
major: Symbol table
minor: Object not found
#002: ../../src/H5Gtraverse.c line 861 in H5G_traverse(): internal path traversal failed
major: Symbol table
minor: Object not found
#003: ../../src/H5Gtraverse.c line 641 in H5G_traverse_real(): traversal operator failed
major: Symbol table
minor: Callback failed
#004: ../../src/H5Gloc.c line 385 in H5G_loc_find_cb(): object 'dset' doesn't exist
major: Symbol table
minor: Object not found
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: ../../src/H5Dio.c line 234 in H5Dwrite(): can't prepare for writing data
major: Dataset
minor: Write failed
#001: ../../src/H5Dio.c line 266 in H5D__pre_write(): not a dataset
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: ../../src/H5Dio.c line 140 in H5Dread(): not a dataset
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: ../../src/H5D.c line 391 in H5Dclose(): not a dataset
major: Invalid arguments to routine
minor: Inappropriate type
Does someone know what went wrong?
Thank you!
The main routine in your program includes the lines
/* Open an existing dataset. */
dataset_id = H5Dopen2(file_id, "/dset", H5P_DEFAULT);
but there's no evidence from what you have posted that the file in question contains any datasets to open. You seem to have created a file called dset but that's not the same thing as a dataset. From what you've posted your file is empty.
The clue to this is given in the error report which states, inter alia
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: ../../src/H5D.c line 334 in H5Dopen2(): not found
major: Dataset
minor: Object not found
You have to create the dataset as well before accessing it. There is an example for that on the hdf5-page as well:
ftp://www.hdfgroup.org/HDF5/examples/introductory/C/h5_crtdat.c
Short: using H5Screate_simple to create dataspace and H5Dcreate2 to create the dataset.

Resources