我想加载代理的列表,通过分割每行“:”分隔符,然后将其加载到一个结构:加载文件内容到结构
struct proxy_data {
ushort type;
char *ip;
uint16_t port;
};
void load_proxies(char * proxy_file)
{
FILE * in;
struct proxy_data * temp = NULL;
if((in = fopen(proxy_file, "rt")) == NULL)
{
fprintf(stderr, "\nCan't open input file!\n");
exit(0);
}
while(!feof(in))
{
temp = malloc(sizeof(struct proxy_data));
fscanf(in, "%s ",temp->type);
fscanf(in, "%s ",temp->ip);
fscanf(in, "%s ",temp->port);
};
fclose(in);
}
但在编译时,我得到了许多错误。
我怎样才能加载每一行到结构?
你会得到什么错误? – junix
你的'struct'引用了其他地方分配的内存('proxy_data.ip'),因此将这个'struct'写入程序的一个实例并在另一个实例中读取将导致对未分配内存的无效引用,并且只会导致崩溃。你需要编组你的数据来写出指针的内容,并想出一种确定数据长度的方法。你不能像你试图的那样去做。 – trojanfoe
@alk它来自哪里?它是'struct proxy_data'的'ip'成员。 – trojanfoe