c

What is the best way to shift data by X bits into and out of a file?

独自空忆成欢 提交于 2021-02-20 05:13:18
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

What is the best way to shift data by X bits into and out of a file?

限于喜欢 提交于 2021-02-20 05:12:29
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

mmap substitute for malloc

浪子不回头ぞ 提交于 2021-02-20 05:00:07
问题 I need to find a way to use mmap instead of malloc. How is this possible? (I am not using libc only syscalls) And yes brk() is possible. I used sbrk() but realized its not sys-call... (x86 inline assembly) I've been looking around and saw this: How to use mmap to allocate a memory in heap? But it didn't help for me, because I had a segfault. Basically, all I want to do a create 3 slabs of memory for storing characters. Say, char * x = malloc(1000); char * y = malloc(2000); char * z = malloc

C: scanf input single character and validation

这一生的挚爱 提交于 2021-02-20 04:53:11
问题 I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works... The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value. my code is: char GetStuff(void) { char c; scanf("%c", &c); while(c != 'A' || c != 'P') { printf("invalid input, enter again (A for AM or P for PM): "); scanf ("

C: scanf input single character and validation

江枫思渺然 提交于 2021-02-20 04:52:02
问题 I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works... The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value. my code is: char GetStuff(void) { char c; scanf("%c", &c); while(c != 'A' || c != 'P') { printf("invalid input, enter again (A for AM or P for PM): "); scanf ("

How to prevent GetOpenFileName from changing the current directory while the dialog is shown?

为君一笑 提交于 2021-02-20 04:26:06
问题 GetOpenFileName (for questionable reasons) changes the current directory of an application while the dialog is shown. This can be reset on dialog closure by specifying OFN_NOCHANGEDIR as dialog initialization flag: OFN_NOCHANGEDIR Restores the current directory to its original value if the user changed the directory while searching for files. Setting this flag, however, doesn't prevent the function from changing the current directory while the explorer dialog is shown . This is an issue in

Get device filesystem path from dev_t on macOS

♀尐吖头ヾ 提交于 2021-02-20 04:03:03
问题 If I have a 32-bit integer BSD device number dev_t (e.g. 0x1000004) on macOS (Darwin), how can I get the corresponding filesystem path for this device (e.g. "/dev/disk1s4")? 回答1: You have to enumerate the mounted file systems and look for one who's device ID matches. You can use getfsstat() for the enumeration. That fills in struct statfs structures. Compare the field f_fsid.val[0] of each structure to the dev_t you're looking for. If they match, then that struct statfs is the one for the

qsort does not work for double array

爱⌒轻易说出口 提交于 2021-02-20 03:43:22
问题 I try to sort an array of double value using qsort, but it doesn't seems to work. Wonder what has gone wrong here?? #include <stdio.h> #include <stdlib.h> static double compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else if (*(double*)a < *(double*)b) return -1; else return 0; } int main() { int idx; double* sum_least_square_err; sum_least_square_err = (double*) malloc (2500*2500*sizeof(double)); sum_least_square_err[0] = 0.642; sum_least_square_err[1] = 0

qsort does not work for double array

南笙酒味 提交于 2021-02-20 03:42:39
问题 I try to sort an array of double value using qsort, but it doesn't seems to work. Wonder what has gone wrong here?? #include <stdio.h> #include <stdlib.h> static double compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else if (*(double*)a < *(double*)b) return -1; else return 0; } int main() { int idx; double* sum_least_square_err; sum_least_square_err = (double*) malloc (2500*2500*sizeof(double)); sum_least_square_err[0] = 0.642; sum_least_square_err[1] = 0

capturing mouse event in C (linux)

南笙酒味 提交于 2021-02-20 03:42:23
问题 I'm writing a little mouselogger in basic C for linux. I search mouse event in linux/input.h, but I have not found anything about this. struct input_event { struct timeval time; __u16 type; __u16 code; __s32 value; } With the struct input_event , I can capturing which button was pressed on mouse and when she move, but not his position. ( I separate the field value in two __s16 variable, but is not a position). If anyone know a structure where I can access this, or a specific file to listen