2010-05-04 1 views
0

모든 도움을 주신 분들께 감사드립니다. 마지막 문제가 하나 있습니다. 웹 사이트 소스를 char var에 넣은 다음 제품 제목을 읽습니다. 그걸 얻었습니다.) 그러나 neweggs 페이지의 추천 제품 중 하나에서 소스 또는 HTML 만 참여하면 작동합니다. 저는이 프로그램이 충돌하고 있다고 생각합니다. 왜냐하면 3 개의 타이틀을 모두 가져 와서 배열에 넣어야 할 때 어떤 제목을 골라야할지 모르기 때문입니다. 어떤 아이디어? 감사. 여기 파서 코드 :C++, 웹 사이트에서 텍스트 가져 오기, 일부 3

http://paste2.org/p/809045

상관 용액을 크게 받고있다.

/** 
* num_to_next - 
* takes in a pointer to a string and then counts how many 
* characters are until the next occurance of the specified character 
* @ptr: the pointer to a string in which to search 
* @c: char delimiter to search until 
**/ 


int num_to_next(char *ptr, char c) 
{ 
     unsigned int i = 0; 
     for (i = 0; i < strlen(ptr); i++) { 
       if (ptr[i] == c) { 
         return i; 
       } 
     } 
     return -1; 
} 


/** 
* space_to_underscore - 
* this should help to alleviate some problems when dealing with 
* filepaths that have spaces in them (basically just changes all 
* spaces in a string to underscores) 
* @string: the string to convert, yo 
**/ 


int space_to_underscore(char *string) 
{ 
     for (unsigned int i = 0; i < strlen(string); i++) { 
       if (string[i] == ' ') { 
         string[i] = '_'; 
       } 
     } 
     return 0; 
} 

char *file_name = (char *)malloc(sizeof(char *)); // allocate memory for where the app name will be stored 
memset(file_name, 0, sizeof(file_name)); // zero the memory 

char td_one[] = "<ul class="featureCells"><li id="ItemCell" class="cell">"; 

char *pstr = strstr(buffer, td_one) + strlen(td_one) + 6; // buffer is the source 

char *poop = pstr + num_to_next(pstr, '>') + 1; 

int blah = num_to_next(poop, '<'); 

strncpy(file_name, poop, blah); 

// null terminate the string // 
file_name[blah] = '\0'; 

space_to_underscore(file_name); 

MessageBox(NULL, file_name, "Product Name", MB_OK); 

free(file_name); 

답변

0

나는이가 유일한 문제가 있는지 확실하지 않습니다,하지만 ...

먼저 char* filename = (char*)malloc(sizeof(char*)) 할 수 없습니다 (물론, 당신은 할 수 있지만, 당신이 실제로에서 원하는 게 아니에요 당신의 앱). 당신이 당신의 문자열 단지 추상적 인 버퍼를 할당 할 수 없습니다 당신이 그것의 예상 크기를 알고 그래서 당신이 원하는 무엇

char* filename = (char*)malloc(SIZE_OF_YOUR_STRING * sizeof(char));입니다. 실제로, 여기에서는 항상 1과 같기 때문에 sizeof(char)을 쓸 필요는 없지만 때때로이 코드를 작성하는 방법은이 블록이 문자열을 문자 배열로 저장한다는 것을 이해하는 데 도움이 될 수 있습니다.

같은 문제에 관한 또 다른 예제는 char* filename = (char*)malloc(65); - OK이고 65 개 문자 심볼들을 저장하기 위해 메모리 블록을 할당한다. (당신이 memset을하고있는 경우) 우리는 더 이상 갈 경우

, char*는 일반 포인터와 포인터의 크기를 반환 귀하의 경우 sizeof(filename)입니다,하지만 당신의 문자열. 여기서 써야 할 것은 strlen(filename)입니다.

관련 문제