Portál AbcLinuxu, 5. května 2025 21:07
Řešení dotazu:
uint16_t temparr[]
a ve smyčce do něj ukládat části původního pole a ty zapisovat?
#define TA_LEN 1000;
static inline void zkonvertuj_dalsi_cast_pole(uint16_ * temparr, uint32_t * array, size_t index, size_t TA_LEN)
{
...
}
void foo(void)
{
FILE file;
size_t index = 0;
uint16_t temparr[TA_LEN];
...
while(pole_jeste_neni_zpracovano)
{
zkonvertuj_dalsi_cast_pole(temparr, array, index, TA_LEN);
fwrite(temparr, sizeof(temparr), 1, file);
index += TA_LEN;
}
...
}
V příkladu samozřejmě není ošetřená případná chyba fwrite()
nebo stav kdy TA_LEN není soudělné s počtem prvků pole array[]
uint32_t array[10000000]; uint32_t *index; size_t i; for(index=array, i=0; i <= 10000000; i++, index++) { fwrite(*((uint16_t) index), sizefof(uint16_t), 1, FD); } /* este raz a citatelnejsie */ uint32_t array[10000000]; uint32_t *index; uint16_t u16; size_t i; for(index=array, i=0; i <= 10000000; i++, index++) { u16=(uint16_t) *index; fwrite(u16, sizefof(uint16_t), 1, FD); }Ono takto definovane by sa vo funkcie malo vytvarat na zasobniku. A to nemusi az tak v tentokrat u Andruido prospievat. Nechces pripadne allokovat pre beh programu tuto pamet cez malloc.
Ůůůfff. Tohle ale mlčky předpokládá Little Endian, kde se dá jenom tak přetypovat uint32_t*
na uint16_t*
a ty dolní byty tam budou. Na Big Endian tam bude všude nula (horní byty).
Ne, beru zpět, neumím číst. Pointer se tady nepřetypuje, takže je to OK.
Dá se to nějak provést bez tvorby ještě jednoho pole uint16_t o stejné velikosti?
Ano. Například nějak tahle (convert_in_place(...)
):
#include <errno.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> static const size_t SIZE = 100000; typedef uint16_t narrow_array_t[SIZE]; typedef uint32_t wide_array_t[SIZE]; typedef uint16_t (*narrow_array_ptr)[SIZE]; typedef uint32_t (*wide_array_ptr)[SIZE]; //////////////////////////////////////////////////////////////////////////////// static narrow_array_ptr convert_in_place(const wide_array_ptr wide_array) { const uint32_t *source = *wide_array; uint16_t *dest = (uint16_t*)source; const uint16_t *const end = dest + SIZE; while (dest < end) *dest++ = *source++; return realloc(wide_array, sizeof(narrow_array_t)); // free extra memory // return (narrow_array_ptr)wide_array; // keep extra memory } //////////////////////////////////////////////////////////////////////////////// static int dump_array_to_file(const char *const file_name, const narrow_array_ptr narrow_array) { const int output = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); if (output == -1) { fputs(strerror(errno), stderr); return EXIT_FAILURE; } int result = EXIT_SUCCESS; size_t to_write = sizeof(narrow_array_t); uint8_t *buffer = (uint8_t*)narrow_array; while (to_write) { const ssize_t written = write(output, buffer, to_write); if (written == -1) { result = EXIT_FAILURE; fputs(strerror(errno), stderr); break; } buffer += written; to_write -= written; } if (close(output) == -1) { fputs(strerror(errno), stderr); return EXIT_FAILURE; } return result; } int main() { // Allocate and populate an array of 32-bit integers. uint32_t (*const wide_array)[SIZE] = malloc(sizeof(wide_array_t)); if (!wide_array) { fputs(strerror(errno), stderr); return EXIT_FAILURE; } for (size_t i = 0; i < SIZE; ++i) (*wide_array)[i] = i % 16384; // Compact 32-bit integers into 16-bit integers in-place. uint16_t (*const narrow_array)[SIZE] = convert_in_place(wide_array); // Write compacted array to standard output, just for fun. const uint16_t *const end = *narrow_array + SIZE; for (uint16_t *number = *narrow_array; number < end; ++number) printf("%d, ", *number); // unchecked! putchar('\n'); // unchecked! // Write the compacted array into a binary file. int result = dump_array_to_file("/tmp/output", narrow_array); free(narrow_array); return result; }
Pokud se nepletu, tento^^^ kód nezávisí na endianness, protože nedělá žádné podivné bitové operace. Tedy na BE vyrobí binární soubor v BE, na LE vyrobí binární soubor v LE. (A pokud se pletu, hned se mi bude někdo posmívat, takže dobře mi tak.)
Nebo přesvědčit fwrite, aby ukládal jen posledních 16bitů z každého prvku a to ostatní ignoroval (tedy ani nevyplňoval nulama).
Ano, tohle by taky šlo, ale musel by se ten fwrite()
(nebo write()
) volat SIZE
-krát, vždycky na ty 2 byty, což by bylo celkem ošklivé a navíc by to bylo závislé na endianness, tj. muselo by se podle LE/BE správně určit, které 2 byty vypsat.
uint16_t *tmp=(uint16_t*)array; for (i=0;i<10000000;i++,tmp++) *tmp=array[i]; fwrite(array,10000000,sizeof(uint16_t),FD);Načo si tam písal tých ostatných 76 riadkov?
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.