2017-12-19 36 views
0

이 부분은 간단한 오류입니다. 따라서 "쉬지 않고"라는 말을 주저하지 마십시오.XCode strcpy (char * dest, io_name_t src)가 dest에 복사하지 않습니다.

여기 내 코드입니다. 변수가 io_name_t에서 strcpy로 변하기 때문에 데이터가 복사되지 않습니다.

static struct sUSBDevInfo { 
    char devName[200]; 
    char svcPlane[200]; 
    char othPlane[200]; 
    int isScreen; 
} USBDevInfo[99]; 

지금 구조체 채우기 :

더 사용하기 위해 데이터를 저장하는 구조체를 정의 나는 그것이 deviceName이 값을 예상했다고 보여줍니다 디버깅 할 때

int getUSBDevices() 
{ 
    int i = -1; 

    CFMutableDictionaryRef matchingDict; 
    io_iterator_t iter; 
    kern_return_t kr; 
    io_service_t device; 
    io_name_t deviceName; 
    io_string_t pathName; 

    matchingDict = IOServiceMatching(kIOUSBDeviceClassName); 
    if (matchingDict == NULL) 
    { 
     return ("No match"); 
    } 
    /* Now we have a dictionary, get an iterator.*/ 
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); 
    if (kr != KERN_SUCCESS) 
    { 
     return (0); 
    } 

    /* iterate */ 
    while ((device = IOIteratorNext(iter))) 
    { 
     /* do something with device, eg. check properties */ 
       /***Display the device names ***/ 

     kr = IORegistryEntryGetName(device, deviceName); 
     if (KERN_SUCCESS != kr) 
     { 
      deviceName[0] = '\0'; 

     } 
     i++; 
     printf("\ndeviceName:%s",deviceName); 
     strcpy(USBDevInfo[i].devName, deviceName); /* !!! This and the following strcpy leave the dest variable unchanged !!! */ 
     IORegistryEntryGetPath(device, kIOServicePlane, pathName); 
     strcpy(USBDevInfo[i].svcPlane, pathName); 
     IORegistryEntryGetPath(device, kIOUSBPlane, pathName); 
     strcpy(USBDevInfo[i].othPlane, pathName); 
     USBDevInfo[i].isScreen = isScreen(deviceName); 
     IOObjectRelease(device); 
    } 

    /* Done, release the iterator */ 
    IOObjectRelease(iter); 
    return (i); 
} 

(예 deviceName:Root Hub Simulation Simulation)하지만, strcpy를 USBDevInfo[i].devName으로 변경하면 목적지는 변경되지 않습니다 ('\0').

편집.

시도해 보았습니다. 다른 지역 변수 작품을 복사하지만, 글로벌 정적 구조체의 항목 번호 :

char cpy[200];  /* local variable */ 
. 
. 
strcpy(cpy, deviceName);     /* Works - copied to cpy */ 
strcpy(USBDevInfo[i].devName, deviceName); /* Not working */ 
USBDevInfo[i].devName[0] = deviceName[3]; /* Not working */ 

편집 2

아래의 답변을 참조하지 않습니다.

+0

"편집 2"변경 사항이 적용되는 경우 지금도 질문이 있습니까? –

+0

@Michael - 예, 지금 작동 중입니다. – TenG

답변

0

편집 2

구조체의 단지 유형 정의를 포함하는 헤더 파일을 개정.

typedef struct sUSBDevInfo { 
    char devName[400]; 
    char svcPlane[400]; 
    char othPlane[400]; 
    int isScreen; 
} tUSBDevInfo; 

구조체 변수 선언을 C 파일로 이동했습니다.

static tUSBDevInfo USBDevInfo[99]; 

이제 strcpy가 작동합니다.

그래서 구조체 배열 변수를 헤더에 넣으면 코드가 배열의 값을 변경할 수 없지만 c 파일 (전역 변수)에 넣으면 작동합니다.

+0

"Edit 2"를 공식 답변으로 이동해야합니다 .... –