2010-05-04 20 views
0

所以,感谢所有帮助人员,我只是有最后一个问题,我把网站源代码放在char var中,然后阅读产品标题(我有得到的),但它只适用于如果我参加的源代码,或仅neweggs页上的特色产品之一的HTML。我认为这个程序崩溃了,因为当我需要把所有三个标题放到一个数组中时,它不知道选择哪个标题。有任何想法吗?谢谢。下面是解析器代码: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));,所以你不能为你的字符串分配一个抽象缓冲区,你必须知道它的预期大小。实际上,在这里你不必写sizeof(char),因为它总是等于1,但是有时候这种编写代码的方式可以帮助你(或者其他人)理解这个块将字符串存储为字符数组)。

关于同一问题的另一个例子:char* filename = (char*)malloc(65); - 没问题,并会分配一块内存来存储65个字符符号。

如果我们进一步去(你正在做的memset),char*是一个普通的指针,你的情况会回到你的指针的大小sizeof(filename),但不是你的。你应该写的是strlen(filename)