I want to write a program to process and save data in HDF5 (the format, the original data is in).
To learn about HDF5 I am playing with simple commands.
When I try to write data in the following snipped, I always get an error.
#include <hdf5.h>
int main ( void )
{
hid_t file_id; dataset_id;
herr_t status;
int i, j, data[4][6];
for (i = 0; i < 4; i++){
for (j = 0; j < 6; j++)
data[i][j] = i * i * j;
}
file_id = H5Fopen ( "myfile.h5", H5F_ACC_TRUNC, H5P_DEFAULT );
dataset_id = H5Dopen ( file_id, "thatdset", H5P_DEFAULT );
status = H5Dwrite ( dataset_id, H5T_NATIVE_INT,
H5S_ALL, H5S_ALL, H5P_DEFAULT,
data);
status = H5Dclose (dataset_id);
status = H5Fclose (file_id);
}
I compile everything with clang on freebsd
cc myHDF5writer.c -I/usr/local/include -L/usr/local/lib -lhdf5 -g
And when I run it, I get the following error:
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5F.c line 495 in H5Fopen(): invalid file open flags
major: Invalid arguments to routine
minor: Inappropriate type
...
When I try fid = H5Fcreate ( FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); instead of the H5Fopen call, I get what looks to me like a similar error:
...
#000: H4D.c line 298 in H5Dopen2() unable to open dataset
major: Dataset
minor: Can't open object
...
Thanks for your help.
Edit: Typo.
Using the H5F_ACC_RDWR flagg in H5Fopen a file is created but with the following error:
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5F.c line 509 in H5Fopen(): unable to open file
major: File accessibilty
minor: Unable to open file
#001: H5Fint.c line 1498 in H5F_open(): unable to open file: time = Thu Dec 10 19:44:48 2020
, name = '/home/joengel/lecture/c/hdf5/nfile.h5', tent_flags = 1
major: File accessibilty
minor: Unable to open file
#002: H5FD.c line 734 in H5FD_open(): open failed
major: Virtual File Layer
minor: Unable to initialize object
#003: H5FDsec2.c line 346 in H5FD_sec2_open(): unable to open file: name = '/path/to/file.h5', errno = 2, error message = 'No such file or directory', flags = 1, o_flags = 2
major: File accessibilty
minor: Unable to open file
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5D.c line 288 in H5Dopen2(): not a location
major: Invalid arguments to routine
minor: Inappropriate type
#001: H5Gloc.c line 246 in H5G_loc(): invalid object ID
major: Invalid arguments to routine
minor: Bad value
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5Dio.c line 314 in H5Dwrite(): dset_id is not a dataset ID
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5D.c line 337 in H5Dclose(): not a dataset
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.10.6) thread 0:
#000: H5F.c line 671 in H5Fclose(): not a file ID
major: File accessibilty
minor: Inappropriate type
Inspecting the file with h5dump yields h5dump errro: unable to open file
According to https://support.hdfgroup.org/HDF5/doc/cpplus_RM/class_h5_1_1_h5_file.html#ae20ef228e7c2db78d31180d3521319ee,
Valid values of flags include: H5F_ACC_RDWR: Open with read/write access. If the file is currently open for read-only access then it will be reopened. Absence of this flag implies read-only access.
H5F_ACC_RDONLY: Open with read only access. - default
Alright. I found my mistake.
What I wanted to do and did not do was to H5Screate_simple(...)
Related
I a trying to write data into a .ts file continuously in c programming.
File Source: on streaming video
File destination: /cache/test/pull_data.ts
output: file size like below
in 1st iteration pull_data.ts --> file size 1213 bytes
in 2nd iteration pull_data.ts --> file size 1213 + 1213 bytes
in 3ed iteration pull_data.ts --> file size 1213 + 1213 +1213 bytes , --- and so on.
I have written code like below:
#define data_size 1213;
unsigned char sourcebuf[data_size + 1];
int source_size = 0;
FILE * fileopen;
fileopen = fopen("/cache/test/pull_data.ts ", "wb");
if (fileopen == NULL) {
printf("error : pull_data.ts opening file ");
exit(1);
} else {
printf("sucesses : pull_data.ts opening file ");
fwrite(sourcebuf, source_size, 1, fileopen);
}
We use it to get the data continuously from the source. but for me, pull_data.ts size is always showing 1213 bytes. How could I loop this function to update -- > current data + comming data in the pull_data.ts file?
The error is in the mode parameter, as the commentors have already told you.
By putting wb, as specified in the documentation of fopen, you are telling fopen to discard any previous content of the file and start from the beginning instead.
What you want, is the mode parameter ab (a for 'append') instead, which will open the file and put the write pointer to the end of the file.
The b in there means 'binary' which translates to 'Do not treat \n in any special way', which is a very good idea if you're dealing with binary files.
There are a number of other flags as well, such as a+ (open for appending and reading), r for readonly and r+ for reading and writing. All of these will open the file, leaving the contents intact.
I am facing this very strange issue.
I wrote a C-function to read value from path "/sys/kernel/debug/irq_domain_mapping" on my linux runing board. Although the that file has "read" permission for all user/group/other, but my function was still not able to read its content.
Would you please help me with some possibilities for this issue. It 's quite strange and this is the first time I got it.
Thanks in advance.
/* Define the file path */
#define HAL_IRQ_DOMAIN_MAP_FILE "/sys/kernel/debug/irq_domain_mapping" /**< irq map file location and mapping */
/* Open the file with permission read */
FILE *fp = NULL;
fp = fopen(HAL_IRQ_DOMAIN_MAP_FILE, "r");
if (fp == NULL) {
tpLOG_osErr("fopen", HAL_IRQ_DOMAIN_MAP_FILE);
return errno;
}
But can not read the file
Oct 3 01:12:59 fopen /sys/kernel/debug/irq_domain_mapping failed: 'Permission denied'
Although the file has read permission for all
[root#]#ls -la /sys/kernel/debug/irq_domain_mapping
ls -la /sys/kernel/debug/irq_domain_mapping
**-r--r--r-- 1 root root 0 Jan 1 1970 /sys/kernel/debug/irq_domain_mapping**
I think I'm running the same functionality in a C program as on the console, but I have permissions problems setting a GPIO direction.
These console commands, run as a normal user, work fine:
$ echo 436 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio436/direction
... while this code, in a C program (which I intend to have the same effect), fails:
...
int gpioNumber = 436;
/* Successful open... */
FILE *exportNode = fopen("/sys/class/gpio/export", "w");
if (exportNode == NULL) {
printf("Unable to open /sys/class/gpio/export\n");
return CT_GPIO_FAIL;
}
char buffer[100];
sprintf(buffer,"%i",mGpioNumber);
if (fprintf(exportNode, buffer) != (strlen(buffer)))
{
printf("Error writing to /sys/class/gpio/export\n");
return CT_GPIO_FAIL;
}
fclose(exportNode);
/* At this point the node /sys/class/gpio/gpio436 exists - I can see it in the console. */
// Set the pin to be input or output by writing "out" to /sys/class/gpio/gpio[xx]/direction
// I think this is the same file as gets opened in the second shell command.
printf("About to open direction.\n");
sprintf(buffer,"/sys/class/gpio/gpio%i/direction",mGpioNumber);
/* This line fails! */
FILE *directionNode = fopen(buffer, "w");
/* .. and the error is reported. */
if (directionNode == NULL) {
int errnum = errno;
printf("Unable to open %s. Last error: %s\n",buffer,strerror(errnum));
return CT_GPIO_FAIL;
}
This is the error report:
Unable to open /sys/class/gpio/gpio436/direction. Last error: Permission denied
If the direction node hasn't been created, I get a different error, so the reported permissions error seems to be accurately reported.
I run the program from the same console as the shell commands (after unexporting the GPIO, but without any other intervening actions. The program runs without error using sudo.
Why aren't the permissions issues the same inside this C program run from the console, as they are in shell commands? Am I missing a simple code error?
This is all running on a Jetson Xavier NX, Ubuntu 18.04.4 LTS
Well ... It does work as written, with the addition of a few milliseconds of sleep between "export" and trying to open the direction node. Apparently the creation of the direction node isn't synchronous in the write to export, at least on this system.
I apologize for the noise.
I'm trying to write a C program for copying content from a source file to a destination file. I want to check for error by checking if the file exists and if the user can read it before attempting to copy it.
I use access() from unistd.h.
Here's the context, I have a folder :
.
├── src.txt
├── test
└── test.c
test.c is my program for testing if the file exists and if I can read it.
int main(){
char* path = "src.txt";
fprintf(stderr, "%d\n", access(path, F_OK));
fprintf(stderr, "%d\n", access(path, R_OK));
return 0;
}
In this example, as src.file exists and has permissions 664, the result should be :
1
1
But the actual output is
0
0
Even if I use the absolute path instead of relative path leading to src.txt, I get the same result. What disturbs me is if I try to open the file, it works:
test.c
int main(){
char* path = "src.txt";
fprintf(stderr, "%d\n", access(path, R_OK));
fprintf(stderr, "%d\n", access(path, F_OK));
FILE *f = fopen(path, "r");
if (!f) {
fprintf(stderr, "File couldn't be opened.\n");
} else {
fprintf(stderr, "File successfully opened.\n");
fclose(f);
}
return 0;
}
It gives as output :
0
0
File successfully opened.
I'm sure I'm missing something, but I can't see what it is.
The access(2) function returns 0 on success. It returns -1 in case you don't have the requested permissions, or the file does not exist, or the path lookup failed. See the man page. It also sets the errno variable to the appropriate error code. So the output of 0, that you are seeing is correct, because you do have read permissions on the said file.
As an aside, most of the system calls return -1 on error (for example read(2), open(2), write(2) etc) and set the errno variable to reflect the exact error which occurred. In order to take the correct action during an error, you need to check the value of the errno variable.
In this example, as src.file exists and has permissions 664, the result should be : 1 1 ? No It should be 0 0, as from the manual page of access() .
RETURN VALUE
On success (all requested permissions granted, or mode is F_OK and the file exists), zero is returned. On error (at
least one bit in mode
asked for a permission that is denied, or mode is F_OK and the file
does not exist, or some other error occurred), -1 is returned, and
errno is set appropriately
This
fprintf(stderr, "%d\n", access(path, R_OK));
prints 0 as R_OK test whether the file exists and grants read, write, and execute permissions & as you stated src.txt exists & its has permission 0664, hence it prints 0. And this
fprintf(stderr, "%d\n", access(path, F_OK));
also prints 0 as F_OK tests for the existence of the file & its true in your case, hence prints 0.
I am trying to write a simulation program in C where I am appending a file by opening it in append mode. This file is a csv (comma separated values).
I would like to write the headings of my simulation information before I write the actual values so that they don't seem unrelated. Is there an easy way to do this?
For example:
Central Node, System Delay, Bandwidth Requirement
14,240,11
4,285,23
13,300,9
My code looks like this:
void Data_Output(FILE *fp){
struct stat buf;
FILE fd = *fp;
fstat(fd, &buf);
fprintf(stderr,"DEBUG------%d\n",buf.st_size);
}
The output error I get is:
ff.c: In function ‘Data_Output’:
ff.c:296:2: error: incompatible type for argument 1 of ‘fstat’
fstat(fd, &buf);
^
In file included from /usr/include/stdio.h:29:0,
from ff.c:1:
/usr/include/sys/stat.h:148:5: note: expected ‘int’ but argument is of type ‘FILE’
int _EXFUN(fstat,( int __fd, struct stat *__sbuf ));
^
Makefile:7: recipe for target 'ff.o' failed
make: *** [ff.o] Error 1
What am I doing wrong? Should I be typecasting it in order to make it work?
You can check size of a file.
For more info how to get size you can check check this post
You can use the fstat() function to dump the stats to a buffer, which would contain the size of the file under st_size.
fstat() works on an integer low level file descriptor, not a FILE * / stream. You need to obtain the descriptor from the FILE * (fp) and use that.
Try:
int fd = fileno(fp);
fstat(fd, &buf);