我有这个程序,可以解析 RSS 并将其馈送到链接列表中。
#include"util.h"
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<string.h>
void parse_tc(){
struct node *head = NULL;
char *bytes = 0;
struct stat st;
stat("techcrunch.txt", &st);
int size = st.st_size;
FILE *f = fopen("techcrunch.txt", "rb");
bytes = (char*)malloc(size + 1);
size_t nread = fread(bytes,1,size,f);
bytes[nread] = 0;
fclose(f);
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->position = 1;
printf("%d. ", temp->position);
char *a = title_parser_tc(bytes,temp);
head = temp;
for(int i = 2; i<21; i++){
temp = (struct node*)malloc(sizeof(struct node));
temp->position = i;
printf("%d. ", temp->position);
a = title_parser_tc(a, temp);
struct node* temp1 = head;
while(temp1->link != NULL)
{
temp1 = temp1->link;
}
temp1->link = temp;
}
free(bytes);
int holder = 0;
int check = 0;
do {
printf("Enter a number: ");
scanf("%d", &holder);
if(holder<1 || holder > 20){
puts("Invalid input");
check = 1;
}
else{
check = 0;
}
} while(check);
get_feed(holder, head);
}
char* title_parser_tc(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "<title>");
if (ptr) {
ptr += 7;
char *ptr2 = strstr(ptr, "</title>");
if (ptr2) {
char* output = malloc(ptr2 - ptr + 1);
memcpy(output, ptr, ptr2 - ptr);
output[ptr2 - ptr] = 0;
if(strcmp(output,"TechCrunch")!=0){
temp->title = output;
puts(temp->title);
temp->link = NULL;free(output);
char *load = pubdate_parser_tc(ptr2, temp);
return load;
}
else{
char *load = title_parser_tc(ptr2, temp);
free(output);
return load;
}
}
}
return NULL;
}
char* pubdate_parser_tc(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "<pubDate>");
if (ptr) {
ptr += 9;
char *ptr2 = strstr(ptr, "</pubDate>");
if (ptr2) {
char* output = malloc(ptr2 - ptr + 1);
memcpy(output, ptr, ptr2 - ptr);
output[ptr2 - ptr] = 0;
temp->pubdate = output;
free(output);
char *load = description_parser_tc(ptr2, temp);
return load;
}
}
return NULL;
}
char* description_parser_tc(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "<description>");
if (ptr) {
ptr += 13;
char *ptr2 = strstr(ptr, "</description>");
if (ptr2){
char* output = malloc(ptr2 - ptr + 1);
memcpy(output, ptr, ptr2 - ptr);
output[ptr2 - ptr] = 0;
description_cleaner_tc(output, temp);
free(output);
char *load = url_parser_tc(ptr2, temp);
return load;
}
}
return NULL;
}
void description_cleaner_tc(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "&nbsp;");
if (ptr) {
ptr += 10;
char *ptr2 = strstr(ptr, "<a ");
if (ptr2) {
char* output = malloc(ptr2 - ptr + 1);
memcpy(output, ptr, ptr2 - ptr);
output[ptr2 - ptr] = 0;
temp->description = output;
puts(temp->description);
free(output);
}
}
}
char* url_parser_tc(char *bytes, struct node *temp){
char *ptr = strstr(bytes, "href");
if (ptr) {
ptr += 6;
char *ptr2 = strstr(ptr, ">");
if (ptr2) {
char* output = (char*)malloc(ptr2 - ptr);
memcpy(output, ptr, ptr2 - ptr - 1);
output[ptr2 - ptr - 1] = 0;
temp->url = output;
puts(temp->pubdate);
puts(temp->url);
puts("");
free(output);
return ptr2;
}
}
return NULL;
}
我的问题是,对于这个文件 textcrunch.txt
,我的程序在 parse_tc()
中的第 10 个循环附近发生段错误。该程序适用于另一个文件,但该文件给了我一个错误。有什么解决办法吗?
该代码基本上是针对要解析的不同字符串重复的相同函数。
最佳答案
段错误通常意味着取消引用空指针(或指向未初始化内存的指针)。如果您使用的是 GCC 或 Clang,则可以使用 -g
标志重新编译,并通过 gdb
运行生成的程序:
gdb --args ....
r
bt
第一行启动 gdb,提供您通常运行的命令。 r
开始运行,bt
给出从程序因段错误而停止的点开始的回溯。至少这可以让您在代码中找到问题发生的位置。您可以使用打印语句开始进一步调试或在其中添加一些防御性代码。
关于c - 解析链表时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30363475/