libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
writebin.c
1 /*
2 * Copyright (c) 2015-2024 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of DSPL.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "dspl.h"
26 
27 
28 
29 
30 
31 
32 
33 #ifdef DOXYGEN_ENGLISH
34 
126 #endif
127 #ifdef DOXYGEN_RUSSIAN
128 
226 #endif
227 int DSPL_API writebin(void* x, int n, int m, int dtype, char* fn)
228 {
229  int res;
230  FILE* pFile = NULL;
231 
232  if(!x)
233  return ERROR_PTR;
234  if(n < 1)
235  return ERROR_SIZE;
236  if(!fn)
237  return ERROR_FNAME;
238 
239  pFile = fopen(fn, "wb");
240  if(pFile == NULL)
241  return ERROR_FOPEN;
242 
243 
244  if(fwrite(&dtype, sizeof(int), 1, pFile) != 1)
245  {
246  res = ERROR_FWRITE_SIZE;
247  goto exit_label;
248  }
249 
250 
251 
252  if(fwrite(&n, sizeof(int), 1, pFile) != 1)
253  {
254  res = ERROR_FWRITE_SIZE;
255  goto exit_label;
256  }
257 
258  if(fwrite(&m, sizeof(int), 1, pFile) != 1)
259  {
260  res = ERROR_FWRITE_SIZE;
261  goto exit_label;
262  };
263 
264  switch(dtype)
265  {
266  case DAT_DOUBLE:
267  if(fwrite((double*)x, sizeof(double), n*m, pFile) != n)
268  {
269  res = ERROR_FWRITE_SIZE;
270  goto exit_label;
271  }
272  break;
273  case DAT_COMPLEX:
274  if(fwrite((complex_t*)x,
275  sizeof(complex_t), n*m, pFile) != n)
276  {
277  res = ERROR_FWRITE_SIZE;
278  goto exit_label;
279  }
280  break;
281  default:
282  res = ERROR_DAT_TYPE;
283  goto exit_label;
284  }
285  res = RES_OK;
286 exit_label:
287  if(pFile)
288  fclose(pFile);
289  return res;
290 }
291 
#define ERROR_FOPEN
Ошибка открытия файла. Файл с заданным именем не может быть открыт для записи и (или) чтения.
Definition: dspl.h:583
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
int writebin(void *x, int n, int m, int dtype, char *fn)
Сохранить данные в бинарный файл
Definition: writebin.c:227
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
#define ERROR_FNAME
Ошибка в имени файла. Необходимо задать корректное имя файла.
Definition: dspl.h:582
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
#define ERROR_FWRITE_SIZE
Ошибка записи блока данных в бинарный файла. Ошибка возникает, если при записи блока данных в файл,...
Definition: dspl.h:586
#define ERROR_DAT_TYPE
Неверный тип данных. Данная ошибка возникает при сохранении данных в бинарный файл,...
Definition: dspl.h:566