我在將結構寫入共享內存時遇到問題,因爲當我這樣做時,代碼只寫入結構的第一部分。C - 將整個結構寫入共享內存的困難
這裏是我的數據結構:
struct shmData{
char number[4];
char description[1020];
};
這裏是我的代碼:
//Structure Pointer - placed on SHM
struct shmstruct *ptr;
char description_to_write[1024] = {0};
struct shmData *data_ptr;
data_ptr = (struct shmData*) description_to_write;
//Shared Memory init
create_shm(&ptr);
printf("Started\n");
//Define Offsets
//init_shm(ptr);
//#########################
//Check if number exist
if (!(strcmp("NO SUCH CODE\0", get_description(ptr, 6)))) {
//-> If not, get next offset (nput) where to write new data
// sem_wait(&ptr->mutex);
// long offset = ptr->msgoff[ptr->nput];
// printf("OFFSET: %li\n", offset);
// if (++(ptr->nput) > NMESG)
// ptr->nput = 0; /* circular buffer */
// sem_post(&ptr->mutex);
// printf("Buffer: %s", argv[2]);
snprintf(data_ptr->number, 4, "%s", "6");
snprintf(data_ptr->description, 1020, "%s\n", argv[2]);
printf("DATA: %s\n", data_ptr->number);
printf("DATA: %s\n", data_ptr->description);
printf("description_to_write: %i\n", description_to_write);
//strcpy(&ptr->msgdata[offset],);
//sem_post(&ptr->nstored);
//printf("Wrote...\n\n");
} else {
//-> Else get offset of this number and put data to it
}
我應該怎麼寫呢?在上面的代碼中,我試圖簡單地顯示描述數組,但我只獲取數字。
我需要它被寫入到共享存儲器如下:
Number(space)(space)(space)Description
共享內存轉儲看起來像:
00003b0: 3120 2020 496e 7075 7420 4572 726f 720a 1 Input Error.
00003c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
*
00007b0: 3220 2020 4f75 7470 7574 2045 7272 6f72 2 Output Error
00007c0: 0a00 0000 0000 0000 0000 0000 0000 0000 ................
00007d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
從調試器:
description_to_write[0] char 54 '6'
description_to_write[1] char 0 '\000'
description_to_write[2] char 0 '\000'
description_to_write[3] char 0 '\000'
description_to_write[4] char 83 'S'
description_to_write[5] char 111 'o'
description_to_write[6] char 109 'm'
description_to_write[7] char 101 'e'
description_to_write[8] char 84 'T'
description_to_write[9] char 101 'e'
description_to_write[10]char 120 'x'
description_to_write[11]char 116 't'
description_to_write[12]char 10 '\n'
醜陋溶液:
int i = 0;
while(i < 4){
if(data_ptr->number[i] == '\000'){
data_ptr->number[i] = 32;
}
i++;
}
你會得到什麼輸出*完全*? 'data_ptr'從哪裏來? –
對不起,我忘了一些代碼,等一下 – x4k3p
另外,你確定你的意思是'argv [2]'而不是'argv [1]'?如果你確定,你確定'argc'大於或等於三? –