我需要用 C 语言编写一个程序来连接到 Web 服务器并下载其 index.html 文件。我已经正确地完成了这一点,但我在程序的第二部分上遇到了困难,规范说:
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”
虽然我已经完成了启动线程的代码,但我不知道如何获取所有 URL。
这是我希望我的线程执行的伪代码(并且我认为它应该有效):
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.
我尝试过像 strstr 这样的函数,甚至计算文件的大小并尝试在巨大的 for 语句中进行 if 条件,但任何结果都返回了所需的结果。
请记住,我是 C 编程的初学者>。<
最佳答案
这是使用 strstr 从文件中提取链接的方法:
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");
}
}
另请注意,虽然 strstr 是线程安全的,但 Needle 和 haystack 指针必须受到互斥体或信号量的保护。在线程环境中使用它时要小心。
使用 write、fwrite 或 fprintf 编写您最喜欢的文件 I/O 函数,而不是上面代码中的 printf。
关于C:将另一个文本文件中的 URL 保存在 txt 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28938166/