简体   繁体   中英

C: Save on a txt file the URL from another text file

I need to make a program in C that connects to a web server and download its index.html file. I have already done this correctly but I'm struggling on the second part of the program, the specifications says:

If the page contains complete http references, launch a concurrent thread to get that page and save it to disk as a file (as before). A complete reference means starting by ”http://” and ending by ”.html”

While I have already done the code to launch the Thread I don't know how to get all URLs.

This is the pseudocode that I want my thread to execute (and that I think it should work):

Open File;

Read File;

Fill the buffer;

LOOP:
Search for "http://", Save Position1
Search for ".html" from the previous saved position, Save Position2
Save all the string that goes from Save Position1 to Save Position2 in a txt.file using the System Call Write.

I've tried functions like strstr and even calculating the size of the file and trying if conditions in a gigantic for statement but anything returned the desired results.

Please keep in mind that I'm a begginer on C programming >.<

This is how you could use strstr for extracting the links from a file:

char str[32]="http://example.com/index.html";
char *p = strstr(str, "http://"), *q;
if (p != NULL) {
    q = strstr(p, ".html");
    if (q != NULL) {
        for (char *x = p; x < q + 5; x++)
            printf("%c", *x);
        printf("\n");
   }
}

Also note that while strstr is thread-safe, the needle and haystack pointers must be protected by mutexes or semaphores. Be careful when you use it in thread environments.

Instead of printf in the above code, write your favorite file I/O function - using write, fwrite or fprintf.

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