diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 0762097..bcbeb34 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -2445,6 +2445,8 @@ trexio_read_$group_sparse_dset$(trexio_t* const file, /* Read the max number of integrals stored in the file */ rc = trexio_read_$group_sparse_dset$_size(file, &size_max); if (rc != TREXIO_SUCCESS) return rc; + /* Cannot read more data points than there is already in the file */ + if (buffer_size > size_max) return TREXIO_INVALID_ARG_3; switch (file->back_end) { @@ -2524,13 +2526,9 @@ trexio_write_$group_sparse_dset$(trexio_t* const file, trexio_exit_code rc; /* Read the max number of integrals stored in the file */ - rc = trexio_has_$group_sparse_dset$(file); - if (rc == TREXIO_SUCCESS) { - rc = trexio_read_$group_sparse_dset$_size(file, &size_max); - if (rc != TREXIO_SUCCESS) return rc; - } else { - size_max = 0L; - } + rc = trexio_read_$group_sparse_dset$_size(file, &size_max); + if (rc != TREXIO_SUCCESS && rc != TREXIO_DSET_MISSING) return rc; + if (rc == TREXIO_DSET_MISSING) size_max = 0L; switch (file->back_end) { diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index bd90628..374e40e 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -1037,14 +1037,20 @@ trexio_exit_code trexio_text_write_$group_sparse_dset$(trexio_t* const file, FILE* f = fopen(file_full_path, "a"); if(f == NULL) return TREXIO_FILE_ERROR; - /* Get the starting position of the IO stream to be written in the .size file */ - int64_t io_start_pos = (int64_t) ftell(f); - /* Specify the line length in order to offset properly. For example, for 4-index quantities the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char. CURRENTLY NO OFFSET IS USED WHEN WRITING ! - ,*/ - const uint64_t line_length = $group_sparse_dset_line_length$L; + */ + const int64_t line_length = $group_sparse_dset_line_length$L; + + /* Get the starting position of the IO stream to be written in the .size file. + This is error-prone due to the fact that for large files (>2 GB) in 32-bit systems ftell will fail. + One can use ftello function which is adapted for large files. + For now, we can use front-end-provided size_max, which has been checked for INT64_MAX overflow. + */ + //int64_t io_start_pos = (int64_t) ftell(f); + int64_t io_start_pos = size_max * line_length; + //fseek(f, (long) offset_file * line_length, SEEK_SET); /* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */