简体   繁体   中英

C++, Get text from a website, part 3

So, thanks for all the help guys, I am just have one last problem, I am putting the website source in a char var, and then reading the product title (I have gotten that), however it only works if I take part of the source, or only the html from one of the featured products on neweggs page. I think the program is crashing, because it doesnt know which title to pick when I need to get all three titles and put them into an array. Any ideas? Thanks. Here is the parser code:

http://paste2.org/p/809045

Any solution is greatly appreciated.

/**
 * 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);

I'm not sure if these are your only problems, but...

First, you can't do char* filename = (char*)malloc(sizeof(char*)) (well, you can, but that's not what you actually want from your app).

What you want to have is char* filename = (char*)malloc(SIZE_OF_YOUR_STRING * sizeof(char)); , so you can't allocate just an abstract buffer for your string and you have to know the expected size of it. Actually, here you don't have to write sizeof(char) because it always equals 1, but this sometimes this way of writing the code can help you(or somebody else) to understand that this block would store a string as array of chars).

Another example on the same problem: char* filename = (char*)malloc(65); - is ok and will allocate a block of memory to store 65 char symbols.

If we go further (where you're doing the memset ), char* is a plain pointer and sizeof(filename) in your case would return the size of your pointer , but not your string . What you should write here is strlen(filename) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM